php - replace string occurrences

后端 未结 3 655
-上瘾入骨i
-上瘾入骨i 2020-12-31 10:34

I have a string \"First line | second line | third line\" How can I replace | with a new line character?

I\'m trying to use

3条回答
  •  情话喂你
    2020-12-31 11:09

    Using strtr is a tad faster than str_replace or preg_replace.

    echo strtr($string,'|', "\n");
    

    Mind the double quotes around the \n.

    Also, if you want to output HTML, a newline char is not sufficient, you need to replace it with
    tags.

    echo str_replace("|", "
    \n", $string);

提交回复
热议问题