Home >Backend Development >PHP Tutorial >php:怎样将中文字符以规定字符长度分段截取并将每段放入数组元素中解决方法

php:怎样将中文字符以规定字符长度分段截取并将每段放入数组元素中解决方法

WBOY
WBOYOriginal
2016-06-13 10:10:34932browse

php:怎样将中文字符以规定字符长度分段截取并将每段放入数组元素中
如:
$cont = "关于烹饪. 这套厨师类的魔幻小说其实小三已经酝酿很长时间了,但一直都觉得不成熟,就没有下笔.空速即……";
function subpart($cont,10,"utf8"){……}
$array = subpart($cont,10,"utf8");

$array = ([0]=>"关于烹饪. ",[1]=>"这套厨师类",[2]=>"的魔幻小说",……);

请教,subpart()函数应该怎样写?

------解决方案--------------------
function subpart($cont,$l,$utf)
{
$len = mb_strlen($cont, $utf);
for($i; $i $arr[] = mb_substr($cont, $i, $l, $utf);
Return $arr;
}
$cont = '中国人民,中国人民银行';
print_r(subpart($cont, 5, "utf8"));
------解决方案--------------------
$cont = "关于烹饪. 这套厨师类的魔幻小说其实小三已经酝酿很长时间了,但一直都觉得不成熟,就没有下笔.空速即……";

$array = subpart($cont,10,"utf8");

echo '

';<br>print_r($array);<br><br>function subpart($cont, $len, $charset)<br>{<br>	$contlen = strlen($cont);<br>	<br>	for ($i=0; $i	{<br>		$contarr[] = substr($cont,$i,$len);<br>	}<br>	return $contarr;<br>}<br>?><br><font color="#e78608">------解决方案--------------------</font><br>楼上的方法都正确,当然你也可以写正则表达式匹配要分割的字符,比如‘,’‘|’。<br>然后用substr装数组<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn