note 78846 deleted from function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:29:07 +0000 |
| Subject: | note 78846 deleted from function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155123@lists.php.net to get a copy of this message | ||
Note Submitter: buganini at gmail dot com
----
<?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);
}
?>