I have wondered for a long time about what real use recursive functions are besides some comp sci "mumbo-jumbo". We had made some in my C++ classes, but they were always for examples that could be accomplished much more easily with other, simpler methods.
Well, today I was working on a website and ran into a bit of a wall. When the administrators of the site write a description of a product, they want to show a preview of the description in the digest on a home page. This is so people can know a little bit about the product besides its name, and can click on the product to see a whole page with a full description. We use this kind of system a lot on various sites we create in many different forms, such as a news synopsis. Our designers is love digests, and we coders have to deliver that design.
Examples: www.profloorsup.com or www.adbay.com (I did not design the "look" of these sites, just the underlying PHP code.)
Obviously, I can't have previews that are the full size of the description. They need to be small, one liners, and we can't really rely on clients to fill these out well. So, my solution was to trim the string using these two lines of code:
$my_string = substr($my_string,0,strpos($my_string,' ',70));
$my_string = $my_string."...";
The two functions here are built in PHP functions. The internal one, strpos, finds the position of a string, in this case a space, starting 70 characters from the beginning of the string, and then returns that position. The outside function truncates the string starting at position 0 to the position returned. This makes sure that the string is broken up by words instead of just splitting words in half, because it looks for spaces where it can break up the string. Where this is needed is in product digests. The second line adds the ellipses to the end of the string. The above 2 lines were written by me when I first started this job, and I wisely saved them in a PHP Tools file I created on Google Docs for future use.
Here's an example of how this worked.
Original 120 Character String:
After sending it through the truncation lines:
The string has been truncated in between words down to 75 characters, not counting the ellipses.
Well, today I ran into a few problems. If the string is less than 70 characters, PHP will return a warning error. If no space characters are found in $my_string, there will also be a warning error. My first solution only looked for spaces after the specified size. So, if you had a long word like "recursion " and then a space after the 70 character starting mark, the string you got back would actually be 79 characters long. It became an estimation game to try to choose a starting point that would be small enough that any floating words at the end of the starting point wouldn't make the preview string too long.
So, after playing with the settings of these two lines and trying some different checks, I decided to write my own function. I had the idea to make it recursively trim the string down to the right size.
//I wrote this function to recursively strip a string down to smaller size, then return it with an ellipses on the end.
//$full_text is the description, and max is the maximum number of characters you want to end up with not including the ellipses. Default: 30
function preview($full_text,$max=30){
//Get the length of the string first.
$length = strlen($full_text);
//If it's under $max, our job is done.
if($length <= $max){
return $full_text;
}
else {
//We want to break stuff up into words so it looks nicer. Find the last occurence of a space in the string
$position = strrpos($full_text," ");
//If no spaces are found, we have to trim letters.
if($position === FALSE) {
$full_text = substr($full_text,0,$max);
return $full_text."...";
}
else {
//Trim the string to that space
$full_text = substr($full_text,0,$position);
//See if it's small enough to finish
$length = strlen($full_text);
if($length <= $max){
return $full_text."...";
}
//Not small enough, so jump through again.
else {
return preview($full_text,$max);
}
}
}
}
If I run my "Lorem ipsum" string in the first example I get this:
A whole word less than my first solution. The total number of characts is 69, not counting the ellipses. This is a much better target number, AND it will never be more than 70 characters, so we can rely on that size to fit into the site design.
It is not super impressive, I know, but it's the first "useful" recursive function I have written for a real program. It definitely entertained me for 30 minutes or so. Thinking back now, there may even be a built in function that does this. I know there is one that splits words up into an array.
My preview function will start to break down with large amounts of text. Since it basically recurses (is that a word?) at every single space, truncating a large body of text will cause major slow down. I guess next I need to write one that jumps to the middle of the string or just after the max length, and starts there. A hybrid of my two solutions.
This fun kind of "learning/experimenting work" definitely made my day go better, though. Sometimes building the same website over and over and over gets tedious. I like to try things that are new and interesting.
Earlier this morning, I had two moles removed, one from my chin and one from my back. I am extremely afraid of needles, and that was the worst part of the whole experience. The mental picture of him scraping my skin off with a knife, then burning my flesh to seal up the wound was not pleasant either. I could not feel the pain, but I definitely could feel the sensation.
Recent Comments