View Full Version : retrieve substring
munaa80
12-25-2002, 03:01 PM
how can i display substring from a string, for example i want to display quarter the news i have and not display the whole news.
and i want to check that the last word displayed is not cut.
ez4me2c3d
12-25-2002, 05:05 PM
so what's a quarter? 1/4 of the line count? 1/4 of the word count? 1/4 of the available space?
determine that first, then you can write 4 or 5 lines to trim the news variable.
<?php
$news = "Today we have had a Merry Christmas so far.";
$fourth = floor(strlen($news) / 4);
if ($news{$fourth} != " ") {
while ($news{$fourth} != " ") {
$fourth++;
}
}
echo substr($news, 0, $fourth) . ' <a href="more.html">...more</a>' . '<br /><br />';
?>
this works based on the number of letters in the news. also it wont cut off a word it will proceed to the end of the word.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.