2013-06-30 37 views
0

什麼是錯的,我創建正則表達式:鏈接內匹配圖像中的正則表達式

$link_image_pattern = '/\<a\shref="([^"]*)"\>\<img\s.+\><\/a\>/'; 
preg_match_all($link_image_pattern, $str, $link_images); 

我試圖做的是符合所有這些他們已在圖像的鏈接。 但是當我嘗試輸出$link_images它包含的第一個索引中的一切:

<pre> 
    <?php print_r($link_images); ?> 
</pre> 

的標記看起來是這樣的:

陣列 ( [0] =>陣列 ([0] = >「

<p>&nbsp;</p> 

<p><strong><a href="url">Title</a></strong></p> 

<p>Desc</p> 

<p><a href="{$image_url2}"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="{$image_url2}" width="569" height="409"></a></p> 

但輸出匹配的內容時,它的SIM層返回,像這樣在頁面模式加上所有其他標記相匹配的第一個字符串:

<a href="{$image_url}"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="{$image_url}" width="568" height="347"></a></p> 

    <p>&nbsp;</p> 

    <p><strong><a href="url">Title</a></strong></p> 

    <p>Desc</p> 

    <p><a href="{$image_url2}"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="{$image_url2}" width="569" height="409"></a></p>") 
+0

索引0將包含匹配的表達式 – DevZer0

+0

使用整個字符串DomDocument庫來讀取HTML並獲取其數據。 – Prix

+0

[使用preg \ _match匹配IMG標記的SRC屬性匹配]的可能的副本(http://stackoverflow.com/questions/2180255/matching-src-attribute-of-img-tag-using-preg-match) – Anirudha

回答

3

正向

正則表達式可能無法解析HTML的最佳解決方案,但也有情況下,它是唯一的選擇比如你的文本編輯器在搜索&替換表單時沒有「在這裏插入html解析腳本」選項。如果您在實際使用PHP,那麼你會更好使用解析腳本,如:

$Document = new DOMXPath($doc); 
foreach ($Document->query('//a//img')) { 
# do something with it here 
} 

說明

這種格式通常保持你 - 不能-DO是,在正則表達式仇敵遠。它會確保您的定位標記包含img標記。同時防止奇怪(並且非常不可能)的邊緣情況,其中屬性具有看起來像圖像標籤的東西。

<a\b(?=\s|>)  # match the open anchor tag 
(?:='[^']*'|="[^"]*"|=[^'"][^\s>]*|[^>=])* # match the contents of the tag, skipping over the quoted values 
> # match the close of the anchor tag 
<img\b(?=\s|>) # match the open img tag 
(?:='[^']*'|="[^"]*"|=[^'"][^\s>]*|[^>=])*  # match the contents of the img tag, skipping over the quoted value 
> # match the close of the img tag 
<\/a> # matcn the close anchor tag 

PHP代碼示例:

示例文本

注意最後一行具有難看屬性將襯托大多數其它正則表達式。

<p>&nbsp;</p> 
<p><strong><a href="url">Title</a></strong></p> 
<p>Desc</p> 
<p><a href="{$image_url2}"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="{$image_url2}" width="569" height="409"></a></p> 

<p><a href="{$image_url2}" Onmouseover="function(' ><img src=picture.png></a> ');" >I do not have an image</a></p> 

enter image description here

代碼

<?php 
$sourcestring="your source string"; 
preg_match_all('/<a\b(?=\s|>) 
(?:=\'[^\']*\'|="[^"]*"|=[^\'"][^\s>]*|[^>=])* 
> 
<img\b(?=\s|>) 
(?:=\'[^\']*\'|="[^"]*"|=[^\'"][^\s>]*|[^>=])* 
> 
<\/a>/imsx',$sourcestring,$matches); 
echo "<pre>".print_r($matches,true); 
?> 

匹配

[0] => <a href="{$image_url2}"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="{$image_url2}" width="569" height="409"></a> 
-1

也許問題是因爲它匹配的一切,直到最後>

嘗試同樣的方法.+\>部分當你使用回採上"[^\>]+ 這部作品在我的編輯

<a.+><img[^>]+></a>

您的需求,並且你只需要添加一些反斜槓\以前<>/

+0

正則表達式不是解析HTML的方式你有沒有注意到你在過去的幾分鐘/秒內做了多少編輯,更不用說這個問題是重複的。 – Prix

+1

@Prix 1.老實說,我最後一次編輯21分鐘前,你的鏈接--17分鐘前,所以你做了4分鐘後2.嘗試閱讀這個問題之前,他試圖「匹配」,而不是「解析」 3.我可以在5分鐘內完成所需的編輯,並且您最好注意其他 – vladkras

+0

仍然不是正則表達式,他可以使用strpos,他仍然可以使用DomDocument,不,我不是指您的編輯21分鐘前我在提到我的評論期間所做的所有修改都超過了4個,這證明了正則表達式不容易處理解析HTML,在這種情況下,使用DomDocument和匹配提取的字符串甚至使用strpos或類似的選項,如果他比較鏈接。 – Prix