php
  • regex
  • php-shorttags
  • 2013-02-04 79 views 1 likes 
    1

    我用curl解析了一些html代碼。某些網站的html源代碼如下:從html源碼刪除php短標籤

    <div id="content"> 
        some words 
    </div> 
    <?  
        $box_social['dimensioni']="80"; 
         $box_vota=array(); 
        $box_vota["novideo"]=''; 
        $box_vota["nofoto"]=''; 
        $box_vota["id_articolo"]='1003691'; 
        include($_SERVER['DOCUMENT_ROOT']."/incs/box_social.php");  
    ?> 
    <div id="footer"> 
        some words 
    </div> 
    

    如何從html源代碼移除php短標籤?我需要

    <div id="content"> 
        some words 
    </div> 
    <div id="footer"> 
        some words 
    </div> 
    

    而且我使用preg_replace('/<\?(.*?)\?>/','',$html);,但php的短標籤部分仍然存在。

    +0

    你提到你正在用curl來解析它嗎?那麼你通過網絡服務器解析的東西呢?它不會以這種方式工作。 –

    +0

    如果您通過URL獲取PHP代碼,那麼在服務器或服務器配置的另一端會出現嚴重的問題。如果這是第三方網站,那麼您應該立即通知他們,因爲這可能也是他們的安全問題。你不應該過濾這些東西,因爲他們不應該讓服務器首先輸出它。 – SDC

    回答

    1

    此正則表達式你的情況相符:

    $html = htmlspecialchars(preg_replace('/<\?([\w\W]*)\?>/','',$html)); 
    $html = htmlspecialchars(preg_replace('/<\?(.*)\?>/s','',$html)); 
    

    這也符合在PHP的一個以上的塊有:

    $html = htmlspecialchars(preg_replace('/<\?([^\?>]*)\?>/','',$html)); 
    

    FROM PHP.NET

    秒(PCRE_DOTALL) 如果設置了此修飾符,則模式中的點元字符與所有字符(包括)匹配g換行符。沒有它,換行符是 排除。這個修飾符相當於Perl的/ s修飾符。 A 否定類如[^ a]總是匹配換行符 ,與此修飾符的設置無關。

    +0

    在哪裏尋找它? – 2013-02-04 17:16:23

    +0

    代碼工作正常,請參閱demo [here](http://phpfiddle.org/main/code/24u-rjc) – 2013-02-04 17:24:21

    相關問題