Sunday, August 2, 2009

PHP how do you limit the number of characters displayed from a result?

I'm making a search form for my site. It searches a column named 'description'. I was wondering if on the search results page you can limit the number of characters for that field of results. Kinda like on other search engines it will give you a description of what items it found with a "..." after so many characters. How do you do this?

PHP how do you limit the number of characters displayed from a result?
%26lt;?php


if(strlen($myDescription) %26gt; 50) {


$showDescription = substr($myDescription,1,50) . "...";


} else {


$showDescription = $myDescription;


}


echo $showDescription;


?%26gt;





enjoy!
Reply:Use the substr() function:





http://www.php.net/substr





For example, to trim a string $str to 50 characters, you can do:





substr($str, 0, 50);
Reply:Is this more a MySQL question? You mention column, I take it you mean a column in a table.





If so the the key is to use SELECT Blah WHERE input LIKE





See Link below for more detail

birthday cards

No comments:

Post a Comment