Returning segment of text

Calling all PHP heads… 🙂

Ok, I’m messing around with a referrer script in PHP and I seem to have a small problem with the code. In some cases with regards to web based searches coming in, I get a huge ass link as the referrer code. I’d like to shorten it up a bit for aesthetic purposes when the link gets displayed.

So, I’m trying to figure out how to grab a string in PHP and return only a chunk of it if it’s larger than what I would like it to be. For example, a Google search string has a total of 300 characters in it, so I want it to only show the first 100 or so.

In ASP, the code is fairly straightforward, for example:

if Len(foo) >= 100 then
    newFoo = Left(foo,100)
else
    newFoo = foo
end if

If the amount of characters in the string foo is greater than or equal to 100, then just give me the string up to the first 100 characters. Now fiddling around with PHP, I managed to get the code to look like this:

$theCount = strlen($foo);

if ($theCount >= 100) {
    $newFoo = substr($foo,0,100);
    echo $newFoo;
}

else {
    echo $foo;
}

This doesn’t seem to work, though to me at least it seems like it should work. Anyone care to tell me what’s wrong with this whilst I hunt down the reason myself?