For some reason I can't get the "count" function to work in php. All I want to do is count the number of characters that have been stored as a variable and then print the result. Any one have a simple solutions to a simple question? Here is my code: if(isset($dat["Hype"])) { $descri = $dat["Hype"]; print $descri; print count($descri); } Code (markup):
Perfect! That works, obviously. Here comes the second question. I didn't think this through correctly and I want to use that fuction so that I can limit the characters being displayed. I have descriptions on a few products that I want to limit to a specific unified size. I can use the strlen to count the variable now how do I limit what that variable prints due to the results on the count? Thanks, this is helping a ton!
You will want to use substr. Not sure how long you want to limit your string to. $newString = substr("$oldString", 0, 200); PHP: Here's the substr manual. http://php.net/substr
Here is a function I use that is kinder to the description. It will only cut off at the end of a word or a sentence and it will add ellipses to the end. It only takes two variables, the string and the cut off point. $newstring = shorten ($oldstring, length); function shorten($string,$length) { if (strlen($string)>$length) { $cutnum = $length-2; $sample = substr($string,$cutnum,1); while ( ($sample!=' ' AND $sample!='.') AND ($cutnum < $length+10) ) { $cutnum = $cutnum + 1; $sample = substr($string,$cutnum,1); } $string = substr($string,0,($cutnum))."..."; } return $string; } Code (markup):