Jump to content
Updating IPB tonight Read more... ×

Archived

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

Buyocat

Recognizing new lines

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?

Share this post


Link to post
Share on other sites
Try \n instead of \r\n ... and str_replace is a better choice than ereg(i)_replace according to the manual at [a href=\"http://ca.php.net/manual/en/function.str-replace.php\" target=\"_blank\"]http://ca.php.net/manual/en/function.str-replace.php[/a]

Share this post


Link to post
Share on other sites
I'm sorry I forget to mention it, but I tried that first without any luck.

Share this post


Link to post
Share on other sites
Use double quotes or PHP won't "see" that as a newline character.

Also I don't recommend you to use <p></p> as replacement, because they would be an empty element... No big deal, but some xhtml/html validators don't like them.

Share this post


Link to post
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]

Share this post


Link to post
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/

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.