Preg_replace isn't removing span tags properly

Discussion in 'PHP' started by interwho, Dec 11, 2010.

  1. #1
    Hi everyone,

    I can't seem to get preg_replace to remove the contents and tags of span tags in a document. Here is my replace code. The document source ($text) is retrieved through curl.

    
    $text8 = preg_replace('/class=".*?"/ims',"", $text);
    $text7 = preg_replace('/style=".*?"/ims',"", $text8);
    $text6 = preg_replace('/id=".*?"/ims',"", $text7);
    $str = preg_replace("/<span.*?<\/span>/ims","", $text6);
    
    echo $str;
    
    PHP:
    Does anyone know what I'm doing wrong?

    Thanks!
     
    interwho, Dec 11, 2010 IP
  2. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #2
    you will need to escape the special chars using backslash for it to work properly.
     
    olddocks, Dec 12, 2010 IP
  3. cyberfox

    cyberfox Member

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Escape special characters , I always use % instead of backslashes in case there are some in the string .. , as % is rarely used
     
    cyberfox, Dec 12, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Remove the contents including the span tags ?
    $str = preg_replace('#<span[^>]*>(.*)</span>#isU','', $str);
    PHP:
     
    MyVodaFone, Dec 12, 2010 IP
  5. interwho

    interwho Member

    Messages:
    198
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #5
    Thanks @MyVodaPhone; that worked! +1 Rep to all who helped!
     
    interwho, Dec 12, 2010 IP