0
$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
4

6 回答 6

8

用wordwrap试试看。我认为它应该可以解决您的问题。

于 2012-09-04T07:03:59.260 回答
1

试试这个

$newtext = wordwrap($text,12, "<br />");
于 2012-09-04T07:06:41.023 回答
0
$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
于 2012-09-04T07:07:36.647 回答
0

只是用一个例子来扩展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.

于 2012-09-04T07:08:40.593 回答
0

您的预期结果在空间和休息方面似乎非常不一致。听起来你想要一些定制的解决方案,更像是你所追求的:

$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
于 2012-09-04T13:20:29.463 回答
0
<?php
$text = "A very long woooooooooooooooooord. and something";
$newtext = wordwrap($text, 8, "\n", false);

echo "$newtext\n";
?>

上面的示例将输出:

A very
long
woooooooooooooooooord.
and
something
于 2016-12-15T13:17:18.453 回答