SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Using str_replace to add <p> tags to an article

    Can someone point out why extra <p></p> tags are being inserted at the end of each paragraph? Here is my code:

    $paragraphTags = str_replace("\n", "</p>\n<p>", '<p>'.$testimonialText.'</p>');


    Here is the result:

    <p>This is a follow-up to the bulletin we sent on September 12. It has come to our attention that some merchants may have received emails where the sample code snippets had syntax errors. We would like to apologize for the inconvenience and make sure we publish the corrected code snippets. Additionally, some merchants reported that their IPN scripts updated to use HTTP 1.1 get "hung" or take a long time to get a ‘VERIFIED’ response. We are including instructions on how to remedy this issue as well by adding "Connection: close” header in the HTTP request.
    </p>
    <p>
    </p>
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Because you had an extra \n type of line end that existed, but you could not see it, because it displays nothing. They're clever like that.

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,124
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by busboy View Post
    Can someone point out why extra <p></p> tags are being inserted at the end of each paragraph?
    Because you told it to...

    Take this into consideration, let's say you have "This is a test\n" as your $testimonialText.

    Here is what you coded:
    Given: '<p>This is a test\n</p>'
    Replace '\n' with '</p>\n<p>'
    Result: '<p>This is a test</p>\n<p></p>'

    One way to solve it is to use trim()
    PHP Code:
    $paragraphTags str_replace("\n""</p>\n<p>"'<p>'.trim($testimonialText"\n").'</p>'); 

  4. #4
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Is there something else I might be missing? I implemented your example, cpradio, and still it’s showing the extra <p></p> tags.
    Convert your dollars into silver coins. www.convert2silver.com

  5. #5
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    And does it make a difference that I’m coding on a Mac instead of PC?
    Convert your dollars into silver coins. www.convert2silver.com

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,124
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Here is the code I have and it works.
    PHP Code:
    <?php
        $paragraphTags 
    "This is a test\n";
        
    $paragraphTags str_replace("\n""</p>\n<p>"'<p>'.trim($paragraphTags"\n").'</p>');
        
    var_dump($paragraphTags);
    ?>
    You can also try it with preg_replace to replace carriage returns and newlines
    PHP Code:
    <?php
      $paragraphTags 
    "This is a test\r\n";
      
    //$paragraphTags = str_replace("\n", "</p>\n<p>", '<p>'.trim($paragraphTags, "\n").'</p>');
      
    $paragraphTags preg_replace("/(\r\n|\r|\n)/""</p>\n<p>"'<p>'.trim($paragraphTags"\r\n").'</p>');
      
    var_dump($paragraphTags);
    ?>


Bookmarks

Posting Permissions

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