$my_string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
预期成果 :
"Lorem Ipsum <br/> is simply<br/> dummy text<br/>of the printing<br/> and typesetting<br/> industry."
标签应该在第 12 个字符之后添加,而不是像这样:
There are ma<br/>ny variations
尝试一下自动换行。我认为它应该可以解决你的问题。
试试这个
$newtext = wordwrap($text,12, "<br />");
$text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
$newtext = wordwrap($text, 12, "<br />'n");
echo $newtext;
输出:
Lorem Ipsum
is simply
dummy text
of the
printing and
typesetting
industry.
http://www.php.net/manual/en/function.wordwrap.php
只是用一个例子来扩展 Shakti 的答案:
$my_string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
$my_wrapped_string = wordwrap($my_string, 12, '<br />');
然后,$my_wrapped_string
的值将Lorem Ipsum<br />is simply<br />dummy text<br />of the<br />printing and<br />typesetting<br />industry.
您的预期结果在空格和中断方面似乎非常不一致。听起来你想要一些自定义解决方案,是你追求的更多的东西:
$array = $my_string.explode(" ");
counter = 0;
for(index=0; index<array.length; index++){
counter += array[index].length+1;
if(counter > 12){
array[index-1] += "<br>"; //put some error handling here
counter = 0;
}
}
//then convert back to string and do whatever
// - I tested this in JS and it worked fine
<?php
$text = "A very long woooooooooooooooooord. and something";
$newtext = wordwrap($text, 8, "'n", false);
echo "$newtext'n";
?>
上面的示例将输出:
A very
long
woooooooooooooooooord.
and
something