我的问题如下。我有一个这样的字符串:
$string = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
我在一个 100px 宽的容器中输出这个字符串。如果任何单词超过 10 个字符,有什么方法可以在 10 个字符后自动插入空格?
$string = chunk_split($string, 10);
不是一个好的解决方案,因为它在单词中间插入了一个空格。例如:
$string = "This is why chunk_split doesn't work";
$string = chunk_split($string, 10);
echo $string;
// OUTPUT: This is wh y chunk_sp lit doesn' t work
关键是空格字符允许字符串创建一个新行。
期望的输出:
$string = "This is how it should work.";
$string = function_i_am_looking_for($string);
echo $string;
// OUTPUT: This is how it should work.
$string = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
$string = function_i_am_looking_for($string);
echo $string;
// OUTPUT: AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA