outputfunctions.php function trimBody($theText, $lmt=500, $s_chr= n , $s_cnt=2) { (Web design portfolio) The

outputfunctions.php function trimBody($theText, $lmt=500, $s_chr= n , $s_cnt=2) { The function trimBody() will take a text input and return a shortened (trimmed) version for display on a page. If an article is very long, you might want to show only the first couple of sentences, or only the first paragraph. If the article is trimmed, you should end the trimmed text with ellipses ( . . . ). The first parameter, $theText, is the text you want trimmed. The second parameter ($lmt) is the absolute longest text string you want returned, expressed in number of characters. The default value, if none is supplied, is 500 characters. The third parameter ($s_chr) is the stop character. You will trim the document to this character. The default value is a newline (n). The last parameter is $s_cnt, or stop count. Given the stop character (in this case n), you will trim after you reach the number designated by $s_cnt. In this case you default to 2, so you will trim the text after the second newline character. You must first initialize the variables. The variable $pos keeps track of the current position of the character you are examining. The variable $trimmed tells you whether or not the text has been trimmed. $pos = 0; $trimmed = FALSE; You then loop through the text $s_cnt times. If you find the character within the text on the last cycle through, then $trimmed will be true, and $theText will be trimmed to that character. If not, $trimmed will be false, and $theText will contain the full text string. (This might happen if you were trimming the string to the third newline and it was only two paragraphs long, for example.) for ($i = 1; $i <= $s_cnt; $i++) if ($tmp = strpos($theText, $s_chr, $pos+1)) { $pos = $tmp; $trimmed = TRUE; } else { $pos = strlen($theText) - 1; $trimmed = FALSE; break; } } $theText = substr($theText, 0, $pos); If the returned string is now longer than your limit, you trim it to the exact length of the limit. if (strlen($theText) > $lmt) $theText = substr($theText, 0, $lmt); After doing this, you might have cut off part of a word. In the interest of keeping your trimmed text clean, you trim back to the last space, and then set $trimmed to true. $theText = substr($theText, 0, strrpos($theText, )); $trimmed = TRUE; } If you have trimmed the text at all, you add the ellipsis, and return the trimmed text back to where the function was called. 426 Chapter 13
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.