How do you find a line break in php?

12,726

Solution 1

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

http://php.net/explode

:)

Solution 2

Updated:

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

$arry_lines = explode("[\n|\r]", $translate);
Share:
12,726
SDuke
Author by

SDuke

Updated on July 26, 2022

Comments

  • SDuke
    SDuke almost 2 years

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

    $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?

  • Gromski
    Gromski about 13 years
    split is deprecated as of PHP 5.3 and shouldn't be used anymore.
  • dfmiller
    dfmiller over 11 years
    The explode function does not allow a regular expression. Use preg_split instead: $arry_lines = preg_split("/[\r|\n]+/", $translate);
  • dfmiller
    dfmiller over 11 years
    To handle all line delimiters, use a regular expression in preg_split: $arry_lines = preg_split("/[\r|\n]+/", $translate);