note 97126 added to function.strrpos
| From: | andre at admolin dot com | Date: | Fri, 02 Apr 2010 14:57:10 +0000 |
| Subject: | note 97126 added to function.strrpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167656@lists.php.net to get a copy of this message | ||
An usage of strrpos() is to find the last space of a string. With this, we can cut an string without
cutting any word.
<?php
function short_str( $str, $len, $cut = true ) {
if ( strlen( $str ) <= $len ) return $str;
return ( $cut ? substr( $str, 0, $len ) : substr( $str, 0, strrpos( substr( $str, 0, $len ), '
' ) ) ) . '...';
}
?>
Using the function:
<?php
# This is a test st...
echo short_str( 'This is a test string.', 17 );
# This is a test...
echo short_str( 'This is a test string.', 17, false );
?>
----
Server IP: 187.16.23.134
Probable Submitter: 189.5.133.251
----
Manual Page -- http://www.php.net/manual/en/function.strrpos.php
Edit -- https://master.php.net/note/edit/97126
Del: integrated -- https://master.php.net/note/delete/97126/integrated
Del: useless -- https://master.php.net/note/delete/97126/useless
Del: bad code -- https://master.php.net/note/delete/97126/bad+code
Del: spam -- https://master.php.net/note/delete/97126/spam
Del: non-english -- https://master.php.net/note/delete/97126/non-english
Del: in docs -- https://master.php.net/note/delete/97126/in+docs
Del: other reasons-- https://master.php.net/note/delete/97126
Reject -- https://master.php.net/note/reject/97126
Search -- https://master.php.net/manage/user-notes.php