A few days ago my front page started throwing a 500 error, I was confused, and initially thought somehow my DNS had gotten screwy as this domain is still fairly new. Then it appeared to work on other computers, then all my other pages seemed to work. I assumed my computer had just cached the error. Yet, upon restart, and clearing the cache it was still there.
The unthinkable had happened, I had screwed up my code!
A quick peek in sFTP showed that I had a big nasty core dump in my home directory. I knew what was wrong instinctively, I had just started parsing RSS on my homepage to show my recent blog posts. I knew it was largely untested FrankenCode. On Monday I had posted a single image and several words as an entry, my FrankenCode was not smart enough to compute.
I opened up the file that generates the RSS descriptions. Racking my brain for what I possibly could have screwed up. Lines upon lines of potential necessary new code were lining up in my head. And as I was reading through my poor stitched together parser I saw:
<? echo PrintDescription($description,” “,30) ?>
“Oh . . .” I sighed. PrintDescription is a handy function I wrote years ago, and it used to only live in the backend of my website. Mainly because it was an unruly little devil that could either do amazing things or produce a fatal error. When working on the parser I was unhappy with the length of the descriptions in the RSS, I wanted the shorter. So I told PrintDescription to give me 30 words, only problem is that the function doesn’t know what to do if there are only 29 words when it starts.
<?
function PrintDescription ($oldString,$token,$breakEnd) {
$breakCount = 0;
$i = 0;
while ($breakCount != $breakEnd) {
if($oldString[$i] == $token) {
$breakCount++;
}
$newString = $newString . $oldString[$i];
$i++;
}
return $newString;
}
?>
This is a lovely little function, I was quite proud when I wrote it. It takes in a string, a token, and a total number of times that token is to be in the description. The token can only be one character, though it is useful for counting words, as it can evaluate ” “. I have also used it to count sentences by evaluating “.”, so by counting 30 spaces, the function will generate a description of 30 words.
However, a function can only do as it’s told. The function does not know what to do if it is asked to count 30 spaces and there are only 29. In the functions current state it throws a fatal error if it is asked to do a task like this. It is something I never bothered to fix because of the way PrintDescription is used in the backend.
So, I need to ask first if the string being evaluated has enough of the token to even generate a description. I was smart this time, and looked through the PHP database to see if such a thing was already in the PHP library. Indeed, there was such a function, and it is called “substr_count”. One graceful question answered my whole problem ” if (substr_count($oldString,” “) >= $breakEnd)”:
<?
function PrintDescription ($oldString,$token,$breakEnd) {
$breakCount = 0;
$i = 0;
if (substr_count($oldString," ") >= $breakEnd) {
while ($breakCount != $breakEnd) {
if($oldString[$i] == $token) {
$breakCount++;
}
$newString = $newString . $oldString[$i];
$i++;
}
} else {
$newString = $oldString;
}
return $newString;
}
?>