2013-06-29 61 views
-1

輸入PHP正則表達式。如何刪除所有元素錨標籤,但保持錨標記某些href屬性包含JPG | PNG | GIF

<a href="http://mysite.com">My Site</a> 
<a href="http://mysite.com/image.jpg"><img src="http://mysite.com/image.jpg"/></a> 
    <a href="http://mysite.com/image.gif"><img src="http://mysite.com/image.gif"/></a> 
<a href="http://yoursite.com">Your Site</a> 

輸出

<a href="http://mysite.com/image.jpg"><img src="http://mysite.com/image.jpg"/></a> 
<a href="http://mysite.com/image.gif"><img src="http://mysite.com/image.gif"/></a> 

感謝對幫助

+0

@mario,我已經試過非常多,Google搜索和搜索在這裏上。但我沒有發現我正在尋找。 –

+0

[解析和處理HTML/XML?]的可能的重複(http://stackoverflow.com/questions/3577641/parsing-and-processing-html-xml) – Quentin

回答

1

我不是PHP開發人員,但我可以給你一個JavaScript演示,希望它能給你一些幫助:)

var reg=/<a\b[^>]*?href=\"((?!jpg|gif|png).)*?"[^>]*?>.*?<\/a>/gi; 
yourstr=yourstr.replace(reg,''); 
0

說明

這將跳過錨標記中的所有其他屬性,即使它們的值看起來像嵌套在值中的屬性。

<a(?=\s|>)     # validate this is an anchor tag 
(?!       # start look ahead ! must not contain, = must contain 
    (?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*? # move through tag, skipping over quoted or non quoted values 
    \shref="[^"]*(?:jpg|png|gif)"     # find href, capture value including quotes if they exist 
)        # end look ahead 
[^>]*>.*?<\/a>     # capture the entire to the close tag 

enter image description here

PHP代碼示例:

示例文本

音符第二行

<a href="http://mysite.com">My Site</a> 
<a wrongtag=" href='http://mysite.com/image.jpg' " href="http://mysite.com">My Site</a> 
<a href="http://mysite.com/image.jpg"><img src="http://mysite.com/image.jpg"/></a> 
    <a href="http://mysite.com/image.gif"><img src="http://mysite.com/image.gif"/></a> 
<a href="http://yoursite.com">Your Site</a> 

代碼

<?php 
$sourcestring="your source string"; 
echo preg_replace('/<a(?=\s|>) 
(?! # start look ahead ! must not contain, = must contain 
(?:[^>=]|=\'[^\']*\'|="[^"]*"|=[^\'"][^\s>]*)*? # move through tag, skipping over quoted or non quoted values 
\shref="[^"]*(?:jpg|png|gif)" # find href, capture value including quotes if they exist 
) # end look ahead 
[^>]*>.*?<\/a> # actually capture the string 
/imsx','',$sourcestring); 
?> 

匹配

[0] => <a href="http://mysite.com/image.jpg"><img src="http://mysite.com/image.jpg"/></a> 
[1] => <a href="http://mysite.com/image.gif"><img src="http://mysite.com/image.gif"/></a> 
相關問題