你如何在PHP中找到换行符?

SDu*_*uke 5 php arrays string

对不起,我对PHP不太好,我试着找到它,然后把每一行都放在一个数组中

$translate="this
is in
multiple
lines";
$lastBreak=0;
$it=array();
$ci=0;
for ($i=1;$i<strlen($translate);$i++) {
    if strchr(substr($translate,$i,$i),"\n") {
        $it[$ci]=substr($translate,$lastBreak+1,$i-1);
        $ci+=1;
        $lastBreak=$i;
    }
}
Run Code Online (Sandbox Code Playgroud)

救命?

rad*_*sch 5

更新:

它可能会产生意外的换行符,这种变化可能会有所帮助:

$arry_lines = explode("[\n|\r]", $translate);
Run Code Online (Sandbox Code Playgroud)

  • Explode 函数不允许使用正则表达式。使用 preg_split 代替: $arry_lines = preg_split("/[\r|\n]+/", $translate); (3认同)
  • 从 PHP 5.3 开始,[`split` 已弃用](http://php.net/split),不应再使用。 (2认同)