note 78352 modified in function.wordwrap by thiago
| From: | thiago@php.net | Date: | Mon, 18 Aug 2008 17:25:32 +0000 |
| Subject: | note 78352 modified in function.wordwrap by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143596@lists.php.net to get a copy of this message | ||
<?php
/**
* Wordwrap without unnecessary word splitting using multibyte string functions
*
* @param string $str
* @param int $width
* @param string $break
* @return string
* @author Golan Zakai <golanzakaiATpatternDOTcoDOTil>
*/
function _wordwrap( $str, $width, $break ) {
$formatted = '';
$position = -1;
$prev_position = 0;
$last_line = -1;
/// looping the string stop at each space
while( $position = mb_stripos( $str, " ", ++$position, 'utf-8' ) ) {
if( $position > $last_line + $width + 1 ) {
$formatted.= mb_substr( $str, $last_line + 1, $prev_position - $last_line - 1,
'utf-8' ).$break;
$last_line = $prev_position;
}
$prev_position = $position;
}
/// adding last line without the break
$formatted.= mb_substr( $str, $last_line + 1, mb_strlen( $str ), 'utf-8' );
return $formatted;
}
?>
--was--
/**
* Wordwrap without unnecessary word splitting using multibyte string functions
*
* @param string $str
* @param int $width
* @param string $break
* @return string
* @author Golan Zakai <golanzakaiATpatternDOTcoDOTil>
*/
function _wordwrap( $str, $width, $break ) {
$formatted = '';
$position = -1;
$prev_position = 0;
$last_line = -1;
/// looping the string stop at each space
while( $position = mb_stripos( $str, " ", ++$position, 'utf-8' ) ) {
if( $position > $last_line + $width + 1 ) {
$formatted.= mb_substr( $str, $last_line + 1, $prev_position - $last_line - 1,
'utf-8' ).$break;
$last_line = $prev_position;
}
$prev_position = $position;
}
/// adding last line without the break
$formatted.= mb_substr( $str, $last_line + 1, mb_strlen( $str ), 'utf-8' );
return $formatted;
}
http://php.net/manual/en/function.wordwrap.php