Shorten a text string

This piece of code shortens a text string to the specified length on a word basis and adds “…” after. Perfect for use in teasers of lengthy text pieces and so on.

* Shortens a string to the specified number of characters on a word basis.
* Adds "..." to the end of the string if the string was longer than specified.
* Example 1
* $text = "The sky is not the limit, I know 'cause I feel higher today!";
* echo shortenText($text);
* Would print:
* The sky is not the limit, I know 'cause I feel higher today!
* Example 2
* $text = "The sky is not the limit, I know 'cause I feel higher today!";
* echo shortenText($text, 20);
* Would print:
* The sky is not the...
function shortenText($text, $chars = 256)
    // Do not shorten if the text is already
    // shorter that the desired amount.
    if(strlen($text) <= $chars)
        return $text;

    $text = $text." ";
    $text = substr($text,0,$chars);
    $text = substr($text,0,strrpos($text,' '));
    $text = $text."...";

    return $text;

Prevent hot-linking

Hot-linking is the act of someone showing an image from your site on a site that ain’t yours, meaning that you get no visitors or any kind of recognition for having the image on your site, but you’ll still have to pay for it with your bandwidth. That sounds kind of unreasonable, don’t you think?

So let’s learn how to prevent it by adding a few rows in our .htaccess file in the root of our website.

Add the code in the box into your .htaccess file to make it work.

# Anti-hotlinking
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?kerola\.nu/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

The first row checks if the referer (i.e. the place on the Internet your visitor came from) isn’t your own domain.

RewriteCond %{HTTP_REFERER} !^http://(.+\.)?kerola\.nu/ [NC]

The second row checks if the referer isn’t empty, i.e. the user typed in the URL to the image directly in her browser.

RewriteCond %{HTTP_REFERER} !^$

The third row generates a HTTP 403 Error (Forbidden) if the two above conditions were met and the requested file type was one of jpg, jpeg, gif, bmp or png.

RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

So in short, this code denies access to your image if you aren’t requesting it from or directly through the browser window. Exactly what we wanted! :)

Change Caps Lock Into Scroll Lock

The caps lock key is really useless and just a burden in everyday life. You accidentally hit it while typing and come out as a Rageaholic 9000 to the people around you.

So what can you do about this? Disable caps lock entirely, you say? Well, that is possible, but if you think about the whole situation for a second there, you’d soon realize that you’d be throwing a whole key away from your keyboard. While playing games (say) I sometimes wish I’d had an extra key right around the regular WASD area, so that I could bind it to something useful. If the whole Caps Lock key is disabled, that can’t be done. Therefore, let’s change the Caps Lock key into an equally useless key, but much more harmless key: Scroll Lock. Scroll Lock is also never being used, but nothing scary happens when you push it.

So let’s combine the Caps Lock’s dreamy position with the harmlessness of the Scroll Lock key shall we?

To do this, you need a mere simple registry hack.

Go to the following place:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout

Change the value of ScanCode Map to the value below, restart your computer and you’re done!

00000000 00000000 02000000 46003A00 00000000

If you’d like a bit more explanation of what this actually does then sure. Basically the ScanCode Map value is just a field wish tells Windows that we want to remap some key into another.

Let’s analyze those numbers a bit, shall we?

We start with

00000000 00000000

This is just here to waste space. Brilliant!

Then we have


This number 2 in here stands for how many keys we are going to remap plus one. In this case, 1 + 1 = 2. Simple enough. The following zeros just waste space, again.

Then we get to the interesting section:


The first four numbers, 4600, stands for the scan code of the key we are going to map to. The last 4 numbers are the scan code of the key we are going to map from.
Luckily for us, 4600 is the scan code for Scroll Lock and 3A00 is the one for Caps Lock. So a little logic processing tells us that we are going to map the keyboard so that whenever you press Caps Lock, it will behave as if you’d pressed Scroll Lock. Just what we wanted! :)

The remaining


just wastes some more space, nothing to worry about.

Don’t forget to restart your computer or it won’t work!

Nice urls with .htaccess

This code turns ugly urls full of parameters into user- and search-engine-friendly browser urls. And it’s totally automatic. This is a win-win-situation :)

Edit your .htaccess file like this if you want your urls to function like this: ->

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
# ->

Or, if you want ->

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
# ->

Just another random page.