本文介绍了php wordwrap切割参数时处理奇怪的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,当使用wordwrap()函数在php与例如汉字。当wordwrap函数中的$ cut参数设置为true时,它会通过插入问号来破坏字符串。
I have a problem when using the wordwrap() function in php with for example chinese characters. When the $cut parameter in the wordwrap function is set to true, it ruins the string by inserting question marks.
有没有解决方案?
推荐答案
本机函数不能安全地用于unicode。以下是 mb_wordwrap
由
<?php
/**
* Multibyte capable wordwrap
*
* @param string $str
* @param int $width
* @param string $break
* @return string
*/
function mb_wordwrap($str, $width=74, $break="\r\n")
{
// Return short or empty strings untouched
if(empty($str) || mb_strlen($str, 'UTF-8') <= $width)
return $str;
$br_width = mb_strlen($break, 'UTF-8');
$str_width = mb_strlen($str, 'UTF-8');
$return = '';
$last_space = false;
for($i=0, $count=0; $i < $str_width; $i++, $count++)
{
// If we're at a break
if (mb_substr($str, $i, $br_width, 'UTF-8') == $break)
{
$count = 0;
$return .= mb_substr($str, $i, $br_width, 'UTF-8');
$i += $br_width - 1;
continue;
}
// Keep a track of the most recent possible break point
if(mb_substr($str, $i, 1, 'UTF-8') == " ")
{
$last_space = $i;
}
// It's time to wrap
if ($count > $width)
{
// There are no spaces to break on! Going to truncate :(
if(!$last_space)
{
$return .= $break;
$count = 0;
}
else
{
// Work out how far back the last space was
$drop = $i - $last_space;
// Cutting zero chars results in an empty string, so don't do that
if($drop > 0)
{
$return = mb_substr($return, 0, -$drop);
}
// Add a break
$return .= $break;
// Update pointers
$i = $last_space + ($br_width - 1);
$last_space = false;
$count = 0;
}
}
// Add character from the input string to the output
$return .= mb_substr($str, $i, 1, 'UTF-8');
}
return $return;
}
?>
这篇关于php wordwrap切割参数时处理奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!