【发布时间】:2017-06-05 15:51:55
【问题描述】:
我有一个字符串,我想在 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>");
}
?>
预期结果:
区分动物的条件和
来自无机物的植物,包括
生长、繁殖、功能的能力
活动,以及死亡前的持续变化。
【问题讨论】:
-
也许你可以用这个正则表达式替换
(\s)\w*$ -
给我一个例子/答案,这样我就可以把它标记为正确的。
-
你的字符串会得到什么结果?
-
@AbraCadaver 我添加了预期结果
标签: php arrays string foreach split