regex
  • preg-replace
  • html-parsing
  • 2011-02-25 46 views 0 likes 
    0

    我需要刪除這些HTML塊之間的內容:PHP的preg_replace HTML內容

    $var1 =" 
    
        <html><head> 
        <meta http-equiv='content-type' content='text/html; charset=ISO-8859-1'></head><body> 
        <img alt='shopozilla' src='http://www.ssopte.com/images/2010/usdos-logo-seal.png' > 
    
        <span style='font-family: Arial,Helvetica,sans-serif; color: rgb(93, 93, 93); 
        font-size: 17px; font-weight: bold;'>shopozilla sent this message to 
    "; 
    
    $var2 = " 
    
        Section 222 of the sand sAct. Section 222(f) provides that the  records of the separtment of State and of diplomatic and consular </font><br> 
        </td></tr></tbody></table></td></tr></tbody></table></body></html> 
    
    "; 
    

    到目前爲止,我試過

    <pre> 
    $content = preg_replace("/$var1(.*)$var2/m", "", $htmlContent); 
    </pre> 
    

    但不工作,所以我需要一個模式/正則表達式應該工作。

    +0

    請參閱:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – 2011-02-25 19:44:26

    回答

    0

    你的模式包含很多在正則表達式中有特殊含義的字符,所以它會混淆preg_replace關於要搜索的內容。只需使用str_replace,因爲你真的不需要這樣的正則表達式。有沒有做PHP在一段時間,但嘗試:

    $pos1 = stripos($htmlcontent, $var1); 
    $pos2 = strripos($htmlcontent, $var2); 
    
    $content = substr_replace($htmlcontent, "", $pos1, $pos2 + strlen($var2)); 
    

    而且有one point that cannot be emphasized enough

    0

    嘗試刪除preg外的圖案。

    $pattern = "/$var1(.*)$var22/m"; //adding /s might help with the /m 
    

    這樣你可以echo $ pattern;並檢查它是否有效。

    相關問題