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
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]
Link to comment
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.
Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.