note 78846 modified in function.wordwrap by thiago
| From: | thiago@php.net | Date: | Mon, 18 Aug 2008 17:25:01 +0000 |
| Subject: | note 78846 modified in function.wordwrap by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143595@lists.php.net to get a copy of this message | ||
<?php
function strwidth($s){
/*
put some fix for ambiguous width hese
*/
$ret = mb_strwidth($s, 'UTF-8');
return $ret;
}
function mb_wordwrap($str, $wid, $tag){
$pos = 0;
$tok = array();
$l = mb_strlen($str, 'UTF-8');
if($l == 0){
return '';
}
$flag = false;
$tok[0] = mb_substr($str, 0, 1, 'UTF-8');
for($i = 1 ; $i < $l ; ++$i){
$c = mb_substr($str, $i, 1, 'UTF-8');
if(!preg_match('/[a-z\'\"]/i',$c)){
++$pos;
$flag = true;
}elseif($flag){
++$pos;
$flag = false;
}
$tok[$pos] .= $c;
}
$linewidth = 0;
$pos = 0;
$ret = array();
$l = count($tok);
for($i = 0 ; $i < $l ; ++$i){
if($linewidth + ($w = strwidth($tok[$i])) > $wid){
++$pos;
$linewidth = 0;
}
$ret[$pos] .= $tok[$i];
$linewidth += $w;
}
return implode($tag, $ret);
}
?>
--was--
function strwidth($s){
/*
put some fix for ambiguous width hese
*/
$ret = mb_strwidth($s, 'UTF-8');
return $ret;
}
function mb_wordwrap($str, $wid, $tag){
$pos = 0;
$tok = array();
$l = mb_strlen($str, 'UTF-8');
if($l == 0){
return '';
}
$flag = false;
$tok[0] = mb_substr($str, 0, 1, 'UTF-8');
for($i = 1 ; $i < $l ; ++$i){
$c = mb_substr($str, $i, 1, 'UTF-8');
if(!preg_match('/[a-z\'\"]/i',$c)){
++$pos;
$flag = true;
}elseif($flag){
++$pos;
$flag = false;
}
$tok[$pos] .= $c;
}
$linewidth = 0;
$pos = 0;
$ret = array();
$l = count($tok);
for($i = 0 ; $i < $l ; ++$i){
if($linewidth + ($w = strwidth($tok[$i])) > $wid){
++$pos;
$linewidth = 0;
}
$ret[$pos] .= $tok[$i];
$linewidth += $w;
}
return implode($tag, $ret);
}
http://php.net/manual/en/function.wordwrap.php