Opposite of Nl2Br? Is It Str_Replace

Opposite of nl2br? Is it str_replace?

Are you writing '\n'? Because \n will only be interpreted correctly if you surround it with double quotes: "\n".

Off topic: the <?= syntax is evil. Please don't use it for the sake of the other developers on your team.

Text Area, nl2br, line breaks galore

in nl2br_lose:

`return preg_replace("/<br\s?\/?>/", '', $string); //removes <br>, <br/>, <br />, <br >`

of course, it exchanges readibility for simplicity. the other option is to write a function to call instead of nl2br:

function stripnl2br($string) {return str_replace("\n", '', nl2br($string));}

New line string \n not being recorgnized by nl2br

48617570747374726173736520315c6e416e737072656368706172746e65723a204d6178204d75737465726d616e6e

According to your hex string it seems that your string does not contain newline. It just \n - two bytes 5c6e where 5c is \ and 6e is n. But new line is 0a

echo (bin2hex("\n")); // 0a

You could replace \n with <br /> using str_replace.

$string = hex2bin('48617570747374726173736520315c6e416e737072656368706172746e65723a204d6178204d75737465726d616e6e');
echo str_replace('\n', "<br/>\n", $string);
// please note, that '\n' and "\n" are different in PHP, '\n' - just two symbols, "\n" - one new line symbol.

Some code: http://3v4l.org/N0FjC#v540

Converting br / into a new line for use in a text area

Try this one

<?php
$text = "Hello <br /> Hello again <br> Hello again again <br/> Goodbye <BR>";
$breaks = array("<br />","<br>","<br/>");
$text = str_ireplace($breaks, "\r\n", $text);
?>
<textarea><?php echo $text; ?></textarea>

nl2br and str_replace not working in PHP script

Something very strange happened because according to the user Touch, his method was working on his computer. Unfortunately it wasn't working on mine! So after a while thinking I came to the conclusion that I was over doing some process of replacement of tags. In order to confirm or not this theory of mine I decided do "back-engineer" Touch's method by erasing line by line and seeing what the result was. In the end I saw that my conclusion was correct, I was over doing process of tag replacement because this code:

$text2 = "../conteudos/start/text2.txt";
if (isset($_POST['body2'])) {
$newData = nl2br($_POST['body2']);
$handle = fopen($text2, "w");
fwrite($handle, $newData);
fclose($handle);
}
// ------------------------------------------------
if (file_exists($text2)) {

$myData2 = file_get_contents($text2);
$myData2 = $myData2;
}

worked in perfection. I can only think that this was due to I was using KCEditor...

A big thanks to all that answered, maing me think and helping me this way to achieve my goal!

How to replace all XHTML/HTML line breaks (br) with new lines?

I would generally say "don't use regex to work with HTML", but, on this one, I would probably go with a regex, considering that <br> tags generally look like either :

  • <br>
  • or <br/>, with any number of spaces before the /


I suppose something like this would do the trick :

$html = 'this <br>is<br/>some<br />text <br    />!';
$nl = preg_replace('#<br\s*/?>#i', "\n", $html);
echo $nl;

Couple of notes :

  • starts with <br
  • followed by any number of white characters : \s*
  • optionnaly, a / : /?
  • and, finally, a >
  • and this using a case-insensitive match (#i), as <BR> would be valid in HTML


Related Topics



Leave a reply



Submit