Looking for a way to limit the length of a text string in PHP? Well here is a simple way to do it.
In this example we will be using the PHP substr function.
Let’s 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.
<?php $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)."...";
In conclusion, the PHP substr function is a simple and effective way to limit the length of a text string. By specifying the start and length parameters, you can easily create a short excerpt from a longer text.
Any questions, just let me know