我有一个字符串,我想在50个长字符串的最后一个空白处换行。
<?php
$str = "the condition that distinguishes animals and plants from inorganic matter, including the capacity for growth, reproduction, functional activity, and continual change preceding death.";
$a = str_split($str, 50);
foreach ($a as $value) {
$lastspace = strrpos($value, " ");
echo chunk_split($value, $lastspace, "<br>");
}
?>
预期结果:
一种将动植物与
无机物区分开来的条件,包括
生长,繁殖,功能
活性和死亡前持续变化的能力。
它并不总是完美的(对于更好的实现,只需搜索wordwrap
),但这通常效果很好:
echo wordwrap($str, 50, '<br>');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句