Jump to content


Photo

Recognizing new lines


  • Please log in to reply
7 replies to this topic

#1 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 11 June 2006 - 11:08 PM

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?
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 11 June 2006 - 11:19 PM

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]
Legend has it that reading the manual never killed anyone.
My site

#3 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 11 June 2006 - 11:20 PM

I'm sorry I forget to mention it, but I tried that first without any luck.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 12 June 2006 - 04:04 AM

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.
~ D Kuang

#5 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 12 June 2006 - 03:44 PM

[!--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]

#6 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 12 June 2006 - 07:16 PM

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/
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#7 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 12 June 2006 - 07:27 PM

Try this:

<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>';
    }

?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#8 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 12 June 2006 - 07:36 PM

Thanks effigy that did the trick.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users