Remove <p>, </p> and everything between ?

Discussion in 'PHP' started by RogerDodgr, Jun 13, 2010.

  1. #1
    Hello,
    This code seems to remove almost everything between the <p> and </p> tag but not inner tags within the p-tags. A lesser problem is that it does not remove the <p> tags themselves.
    
    $output = preg_replace('#(<p[^>]*>).*?(</p>)#', '$1$2', $output)
    
    Code (markup):
    I need a snippet of code that will remove everything including the <p>,</p> tags and also remove all inner tags.
     
    RogerDodgr, Jun 13, 2010 IP
  2. Scripts man

    Scripts man Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    its easy
    use striptag
     
    Scripts man, Jun 13, 2010 IP
  3. RogerDodgr

    RogerDodgr Well-Known Member

    Messages:
    267
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Scripts_man, I ran across striptag as I was googling: http://php.net/manual/en/function.strip-tags.php

    If I understand correctly, I have to specify every possible tag I would want to keep,,, not the one I want to get rid of. This is directly from the page:
    
    // Allow <p> and <a>
    echo strip_tags($text, '<p><a>');
    
    PHP:
    The above example will output:
    <p>Test paragraph.</p> <a href="#fragment">Other text</a>
     
    RogerDodgr, Jun 13, 2010 IP
  4. Scripts man

    Scripts man Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you know
    you can use the jquery
    in that
    js i mean
     
    Scripts man, Jun 14, 2010 IP
  5. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #5
    This does what you have asked for above.

    <?php
    $str = '<p> hello <b>world</b></p><br />this is a test';
    $f=preg_replace("/<p>(.*)<\/p>/", "", $str);
    print $f;
    ?>
    PHP:
    OUTPUT: <br />this is a test
     
    lukeg32, Jun 14, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    Your old code:
    
    $output = preg_replace('#(<p[^>]*>).*?(</p>)#', '$1$2', $output)
    
    Code (markup):
    You need to backslash closing tags : like <\/p>
    Try this, it should remove everything between and including the p tags replacing with your $1$2 variables
    
    $output = preg_replace('#<p[^>]*>(.*)<\/p>#', '$1$2', $output);
    
    Code (markup):
     
    MyVodaFone, Jun 14, 2010 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    Is it really necessary for you to repeat answers already given ?
     
    MyVodaFone, Jun 15, 2010 IP
  8. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Sorry!
    I just saw the first post and clicked the Reply with Quote Directly!!
    I urge i always doing this type of mistakes
    Post Removed!
     
    roopajyothi, Jun 15, 2010 IP