PHP 分割换行符:explode() 和 preg_split() 方法详解
在 PHP 中,可以使用 PHP 内置函数 'explode()' 或者 'preg_split()' 来分割换行符。
方法一:使用 'explode()' 函数
$string = 'This is a string
with
multiple lines.';
$lines = explode("\n", $string);
foreach ($lines as $line) {
echo $line . "<br>";
}
输出:
This is a string
with
multiple lines.
方法二:使用 'preg_split()' 函数
$string = 'This is a string
with
multiple lines.';
$lines = preg_split('/\r\n|\r|\n/', $string);
foreach ($lines as $line) {
echo $line . "<br>";
}
输出:
This is a string
with
multiple lines.
注意:'preg_split()' 函数支持多种换行符,包括 '\r\n'、'\r' 和 '\n'。
原文地址: http://www.cveoy.top/t/topic/lOFL 著作权归作者所有。请勿转载和采集!