note 74763 added to function.strpos
| From: | RichDeeson at osu1 dot php dot net | Date: | Thu, 26 Apr 2007 15:58:31 +0000 |
| Subject: | note 74763 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125892@lists.php.net to get a copy of this message | ||
Here's a somewhat more efficient way to truncate a string at the end of a word. This will end
the string on the last dot or last space, whichever is closer to the cut off point. In some cases, a
full stop may not be followed by a space eg when followed by a HTML tag.
<?php
$shortstring = substr($originalstring, 0, 400);
$lastdot = strrpos($shortstring, ".");
$lastspace = strrpos($shortstring, " ");
$shortstring = substr($shortstring, 0, ($lastdot > $lastspace? $lastdot : $lastspace));
?>
Obviously, if you only want to split on a space, you can simplify this:
<?php
$shortstring = substr($originalstring, 0, 400);
$shortstring = substr($shortstring, 0, strrpos($shortstring, " "));
?>
----
Server IP: 194.145.210.3
Probable Submitter: 80.45.44.15
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/74763
Del: integrated -- https://master.php.net/note/delete/74763/integrated
Del: useless -- https://master.php.net/note/delete/74763/useless
Del: bad code -- https://master.php.net/note/delete/74763/bad+code
Del: spam -- https://master.php.net/note/delete/74763/spam
Del: non-english -- https://master.php.net/note/delete/74763/non-english
Del: in docs -- https://master.php.net/note/delete/74763/in+docs
Del: other reasons-- https://master.php.net/note/delete/74763
Reject -- https://master.php.net/note/reject/74763
Search -- https://master.php.net/manage/user-notes.php