www.webdeveloper.com
Results 1 to 8 of 8

Thread: string replace before adding to MySQL

  1. #1
    Join Date
    Aug 2009
    Posts
    53

    string replace before adding to MySQL

    I'm trying to add a <textarea> to a MySQL database.

    The <textarea> has multiple lines, so I want to replace \r\n with <p></p>

    Here's the <textarea> formatting script:
    PHP Code:
    $entry $_POST['entry'];
    $entry "<p>".$entry."</p>";
    $entry str_replace("\r\n""<p></p>"$entry); 
    It returns, "**line 1**\r\n**another line**"

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,251
    At a guess, the browser is only sending "\n" as the newline, not "\r\n". Also, you probably want to transpose the order to "</p><p>". You could use a regexp to handle any case, including multiple consecutive newlines:
    PHP Code:
    $html "<p>" preg_replace('/[\r\n]+/'"</p>\n<p>"$text) . "</p>"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2009
    Posts
    53
    Still didn't work.

    I typed this into the textarea:
    text
    text

    it returned:
    Text\r\nText

    I check in the DB & it says:
    <p>Text\r\nText</p>

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,251
    Works for me:
    PHP Code:
    <?php
    $text 
    "text
    text

    text"
    ;

    $html "<p>" preg_replace('/[\r\n]+/'"</p>\n<p>"$text) . "</p>";
    ?>
    <pre><?php
    echo htmlspecialchars($html);
    ?></pre>
    Output:
    Code:
    <p>text</p>
    <p>text</p>
    <p>text</p>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2009
    Posts
    53
    By typing:
    Test

    Test

    It displays:
    <p>Test\r\n\r\nTest</p>

    the DB:
    &lt;p&gt;Test\r\n\r\nTest&lt;/p&gt;

  6. #6
    Join Date
    Aug 2009
    Posts
    53
    I took out the htmlspecialchars() bit & the &lt; &gt; bit is fixed, now it's just the \r\n part.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,251
    I have no idea from the code and descriptions provided how it would happen, but what you are showing us seems to indicate that you do not have carriage return and newline characters in there, but literally back-slash, "r", back-slash, "n" -- which is not the same thing as an actual carriage return and newline which are represented in PHP by "\r\n" when within double quotes.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Dec 2009
    Location
    Denmark
    Posts
    240
    Don't know why you want to replace line break with a P tag since the P tag makes a double line break.

    But if you want a P tag, you will need to wrap it around the text.
    Something like this.
    PHP Code:
        $mystr str_replace("\r"""$mystr);
        
    $mystr preg_replace("/((.*)+)\n/""<p>$1</p>"$mystr); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles