Code Snippet
Convert BR to Newline
Technique #1
function br2newline( $input ) {
$out = str_replace( "<br>", "\n", $input );
$out = str_replace( "<br/>", "\n", $out );
$out = str_replace( "<br />", "\n", $out );
$out = str_replace( "<BR>", "\n", $out );
$out = str_replace( "<BR/>", "\n", $out );
$out = str_replace( "<BR />", "\n", $out );
return $out;
}
Converts a break tag to a newline - no matter what kind of HTML is being processed.
Technique #2
function br2nl( $input ) {
return preg_replace('/<br(\s+)?\/?>/i', "\n", $input);
}
Technique #2 will work a lot better than #1
But in #1, you could just use the first three calls and use str_ireplace() instead.
I’m suffered a lot. A used ” but I should use ‘. Could it be really a mistake?
please help me out why is “i” used in
function br2nl( $input ) {
return preg_replace(‘//i’, “\n”, $input);
}
at the end of first parameter of preg_ replace
function br2nl( $input ) {
return preg_replace('//i', "\n", $input);
}
sorry the code got messed up
You can also do:
function _br2nl($input)
{
return str_replace(array("", "", "", "", "", ""),"\n", $input );
}
<pre><code>
function _br2nl($input)
{
return str_replace(array(“<br>”, “<BR>”, “<br/>”, “<BR/>”, “<br />”, “<BR />”),”\n”, $input );
}
</code></pre>
`(\s+)?` could just be `\s*`.
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.