Obtain first line of a string in PHP

后端 未结 11 1137
醉话见心
醉话见心 2021-02-01 13:14

In PHP 5.3 there is a nice function that seems to do what I want:

strstr(input,\"\\n\",true)

Unfortunately, the server runs PHP 5.2.17 and the

11条回答
  •  攒了一身酷
    2021-02-01 13:49

    A quick way to get first n lines of a string, as a string, while keeping the line breaks.

    Example 6 first lines of $multilinetxt

    echo join("\n",array_splice(explode("\n", $multilinetxt),0,6));
    

    Can be quickly adapted to catch a particular block of text, example from line 10 to 13:

    echo join("\n",array_splice(explode("\n", $multilinetxt),9,12)); 
    

提交回复
热议问题