fritz Jung
07-24-2002, 09:03 PM
Greets,
I can't figure out how to do a string replace that will result in displaying say... JUST the FIRST 20 words of a field...
help?
Dalsor
07-24-2002, 10:33 PM
If I understand the question correctly, try something like this...
if ( strlen( $title ) > 18 )
{
$title[19] = ".";
$title[20] = ".";
$title[21] = ".";
$title = substr( $title, 0, 21 );
}
I know there's a more elegant way to do this, just can't think of it off the top of my head.
firepages
07-25-2002, 03:18 AM
If your field is coming from a DB, eg MySQL then you are better served grabbing a SUBSTRING from that field, although that will not guarentee a complete final word!
The only way I can think of (and its quite messy) is..
<?
/*just grab the ist 5 words*/
$str="these are some words there are not many of them";
$temp=explode(" ",$str);
for($x=0;$x<5;$x++){
$newstr .=$temp[$x].' ';
}
echo $newstr;
?>
you could use split() or preg_split() as well but that would be a little heavy handed I think
Centaur
07-27-2002, 11:33 AM
if (preg_match("/^((\\w+\\W+){0,19}\\w+)/", $string, $matches)) {
echo "<p>First 20 or less words: ". $matches[1]. "</p>";
}