我想让这个单词的前15个字符支持留白。
$word = 'my word'n'nis this my word?'nMagnum'nwtwsetst.'nwtvet'n'n#rp';
结果应该是:
"my word
is thi..."
使用substr()
函数
$word = 'my word'n'nis this my word?'nMagnum'nwtwsetst.'nwtvet'n'n#rp';
echo substr(str_replace(''n', "'n", $word), 0, 15);
虽然我在你的一个评论中看到,你说这不会保留空白。它会的,但是你没有看到它。
如果您在/to HTML中重复此内容,我认为您是,并且您不在<pre>
标记中,请尝试将换行符'n
转换为<br />
。
,
$word = 'my word'n'nis this my word?'nMagnum'nwtwsetst.'nwtvet'n'n#rp';
echo nl2br(substr(str_replace(''n', "'n", $word), 0, 15));
您需要将字符串放在"
中以转义字符工作并使用substr
功能,试试这个:
$word = "my word'n'nis this my word?'nMagnum'nwtwsetst.'nwtvet'n'n#rp";
echo substr($word, 0, 15);
演示链接