Php - replace entry strings

I have the line "First line | second line | third line" How to replace | to the new line character?

I am trying to use preg_replace but without liuck

+8
string php
source share
3 answers

here

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

when \ n is placed in a double line Qouted, it changes to a new line

+17
source share

Use this:

 str_replace('|', PHP_EOL, $str); 

You should use PHP_EOL instead of "\n" because PHP_EOL will always work on all server platforms. (NB. Windows uses "\r\n" and unix / linux uses "\n" ).

+3
source share

Using strtr less than str_replace or preg_replace .

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

Note the double quotes around \n .

In addition, if you want to output HTML, then char is not enough for a new line, you need to replace it with <br /> tags.

 echo str_replace("|", "<br />\n", $string); 
+2
source share

All Articles