如何在php中找到换行符?

时间:2022-07-07 21:30:24

Sorry I ain't that good with PHP yet, I tried this to find it and then put each line in an array

不好意思,我用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;
    }
}

help?

帮助吗?

3 个解决方案

#1


16  

$it = explode("\n", $translate);

http://php.net/explode

http://php.net/explode

:)

:)

#2


2  

Updated:

更新:

it can produce an unexpected linebreak, this variation may help:

它可以产生一个意想不到的线裂,这种变化可能有助于:

$arry_lines = explode("[\n|\r]", $translate);

#3


0  

You can do a $string = nl2br($string) so that your line break is changed to

可以执行$string = nl2br($string),以便将换行符更改为

<br />. 

Then you can manipulate the string, e.g. split it up by the first occurance of

然后你可以操纵绳子,例如把它按第一次出现的情况分开

<br />

like this:

是这样的:

list($first, $second) = explode('<br />', $string, 2);

#1


16  

$it = explode("\n", $translate);

http://php.net/explode

http://php.net/explode

:)

:)

#2


2  

Updated:

更新:

it can produce an unexpected linebreak, this variation may help:

它可以产生一个意想不到的线裂,这种变化可能有助于:

$arry_lines = explode("[\n|\r]", $translate);

#3


0  

You can do a $string = nl2br($string) so that your line break is changed to

可以执行$string = nl2br($string),以便将换行符更改为

<br />. 

Then you can manipulate the string, e.g. split it up by the first occurance of

然后你可以操纵绳子,例如把它按第一次出现的情况分开

<br />

like this:

是这样的:

list($first, $second) = explode('<br />', $string, 2);