note 97126 deleted from function.strrpos by tularis
| From: | tularis@php.net | Date: | Tue, 10 Jan 2012 20:58:17 +0000 |
| Subject: | note 97126 deleted from function.strrpos by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185556@lists.php.net to get a copy of this message | ||
Note Submitter: andre at admolin dot com
----
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 );
?>