EDIT 2: I come up with this kind of dirty solution, 这是一个创建临时数组的函数。它将包含要添加换行符的字符串段。然后我们使用内爆将其设置回字符串。
function breakLines($str,$line_length=30,$implodechar=\'<br>\')
{
$str_length = strlen($str);
$i=0;
$o=0;
$tmp = array();//temporary array
for ($j=0; $j<$str_length; $j++)
{
//if we are at the end of line and
//that the current char is a space or punctuation char
if($i>=$line_length && preg_match(\'/[\\s\\,\\;\\.\\:]/isu\', $str[$j]))
{
$i++;
$tmp[]=trim(substr($str,$o,$i));//"\\r\\n";
//we update the offset
$o+=$i;
//we reset our counter
$i=0;
}else{
$i++;
}
}
return implode($implodechar,array_filter($tmp));
}
因此,您可以像这样简单地使用它:
echo "Lyrics: ".breakLines($this->get_lyrics($artist, $song))."</div>";
希望有帮助:)
EDIT: oooops, sorry I understood the contrary of what you are trying to achieve.
使用
preg_replace 具有查找换行符的模式的函数?
echo "Lyrics: ".preg_replace(\'/(\\R+)?/isu\', \'\',$this->get_lyrics($artist, $song))."</div>";
我添加了较低的“u”修饰符来处理编码。
您可以在此处进行测试:http://www.phpliveregex.com/p/ns1#preg-replace