function mb_eaw_wrap($string, $width = 75, $break = "\n", $table = null, $encoding = null) { if (func_num_args() < 5) { $encoding = mb_internal_encoding(); } $len = mb_strlen($string, $encoding); if ($len === false) { return false; } $line = ''; $lineLen = 0; $retval = ''; for ($start = 0; $start < $len; ++$start) { $char = mb_substr($string, $start, 1, $encoding); $charwidth = mb_eaw_strwidth($char, $table, $encoding); if ($lineLen + $charwidth <= $width) { $line .= $char; $lineLen += $charwidth; } else { $retval .= $line . $break; $line = $char; $lineLen = $charwidth; } } if ($line !== '') { return $retval . $line . $break; } return $retval; }
function mb_eaw_strimwidth($str, $start, $width, $trimmarker = '', array $table = null, $encoding = null) { if ($width < 0) { trigger_error('Width is negative value', E_USER_WARNING); return false; } if (func_num_args() < 6) { $encoding = mb_internal_encoding(); } $strLen = mb_strlen($str, $encoding); if ($strLen === false) { return false; } if ($start < 0 || $start > $strLen) { trigger_error('Start position is out of range', E_USER_WARNING); return false; } $str = mb_substr($str, $start, $strLen, $encoding); if ($str === false) { return false; } $strLen -= $start; $eawa = mb_eaw_strwidth_array($str, $table, $encoding); if ($eawa === false) { return false; } if (array_sum($eawa) <= $width) { return $str; } $eawa_marker = mb_eaw_strwidth($trimmarker, $table, $encoding); if ($eawa_marker === false) { return false; } if ($eawa_marker > $width) { trigger_error('Trimmarker is out of range', E_USER_WARNING); return false; } $width -= $eawa_marker; $charLen = 0; for ($start = 0; $start < $strLen; ++$start) { if ($width - $eawa[$start] < 0) { break; } ++$charLen; $width -= $eawa[$start]; } return mb_substr($str, 0, $charLen, $encoding) . $trimmarker; }
function mb_eaw_str_pad($mbstring, $pad_length, $pad_sbstring = ' ', $pad_type = STR_PAD_RIGHT, array $table = null, $encoding = null) { $eaw = func_num_args() < 6 ? mb_eaw_strwidth($mbstring, $table) : mb_eaw_strwidth($mbstring, $table, $encoding); return str_pad($mbstring, $pad_length + (strlen(bin2hex($mbstring)) / 2 - $eaw), $pad_sbstring, $pad_type); }