• May 14, 2018
    $your_array = explode("\n", $your_string_from_db);

    For instance, if you have this piece of code :

    $str = "My text1\nMy text2\nMy text3";
    $arr = explode("\n", $str);
    var_dump($arr);

    You’d get this output :

    array
      0 => string 'My text1' (length=8)
      1 => string 'My text2' (length=8)
      2 => string 'My text3' (length=8)

    Note that you have to use a double-quoted string, so \n is actually interpreted as a line-break.
    (see that manual page for more details)

    หรือ

    A line break is defined differently on different platforms, \r\n, \r or \n.

    Using RegExp to split the string you can match all three with \R

    So for your problem:

    $array = preg_split ('/$\R?^/m', $string);

    That would match line breaks on Windows, Mac and Linux!

    หรือ

    I’ve always used this with great success:

    $array = preg_split("/\r\n|\n|\r/", $string);

    (updated with the final \r, thanks @LobsterMan)

    อ่านเพิ่ม

    https://stackoverflow.com/questions/1483497/how-to-put-string-in-array-split-by-new-line

    https://stackoverflow.com/questions/23955393/php-divide-string-by-new-line-n-character

    <?php
        $my_array = array("stuff1", "stuff2", "stuff3");
        foreach ( $my_array as $item ) {
            echo $item . "<br/>";
        }
    ?>


เวอไนน์ไอคอร์ส

ประหยัดเวลากว่า 100 เท่า!






เวอไนน์เว็บไซต์⚡️
สร้างเว็บไซต์ ดูแลเว็บไซต์

Categories