Jump to content

Recognizing new lines


Buyocat

Recommended Posts

I would like to take a chunk of text gathered from a form and replace new lines with </p><p> or something along those lines so that I submitted data can retain its paragraph structure. I know that nl2br does something similar but that's not quite what I want. I tried to eregi_replace('\r\n', '</p><p>', $text) but that didn't work, so I guess what I'd like to know is what symbol am I looking for, or is there another way or material I can look at for guidance?
Link to comment
https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Also I don't recommend you to use <p></p> as replacement,[/quote]

-just replace with '</p><p>' not the other way around and then print the extra tags before and after:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]echo '<p>' . $converted_string . '</p>';[/quote]
Link to comment
https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/#findComment-44631
Share on other sites

Well replacing the "\n" with '</p><p>' does work, but as Poirot warned it leads to extra emtpy </p><p> if the user were to say hit enter twice (something that I do, so I can trust others to do). So can anyone think of a decent way to get an xhtml compliant set of p tags around the text but not around whitespace?

ps I tried searching for "\n\n" but that didn't work, and I'd rather not have to use an elaborate if statement, nl2br is enough for the job even if it doesn't satisfy completely/
Link to comment
https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/#findComment-44722
Share on other sites

Try this:

[code]<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea name="txt"></textarea>
<br>
<input type="submit">
</form>

<?php

    if ($_POST) {
        $p_txt = trim($_POST['txt']);
        $p_txt =
            '<p>' .
            preg_replace("/(\r\n)+/", '</p><p>', $p_txt).
            '</p>';
        echo '<pre>', htmlentities($p_txt), '</pre>';
    }

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/11753-recognizing-new-lines/#findComment-44729
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.