Limit Length of Text in PHP

Ok say you have a title like this:
“This is a long title for a story this is a title wow.”
And you want to have a excerpt from it.
Such as “This is a long title” instead of the whole thing

We would use the PHP substr function, which goes like this.
<?
$test = "your text here adding length here";
echo "".substr($test, 0, 25)."";
?>
$test is the text to clip, the 0 is where is starts in the phrase (0 makes it the beginning), while 25 is the amount of letter to clip.

This would make it say “your text here adding len”.

Then you could add to the end off the echo a …
like this:
echo "".substr($test, 0, 25)."...";

Written by Andy Feliciotti

Andy Feliciotti is a Web Designer out of College Park, Maryland. He diggs web design and social media, if it's on the internet he has probably seen it. You can check out what Andy finds daily on his twitter.


One Response to “Limit Length of Text in PHP”

  1. Andy Feliciotti on April 26th, 2009 at 8:01 pm

    Any questions, just let me know

Leave a Comment