2017-04-23 34 views
1

雖然answering a question我發現自己掙扎不是使用regex to parse html不使用正則表達式來解析html

我應該怎樣得到urlstyle="background:url(http...)使用只有一個html解析器?

<a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) no-repeat center center;background-size:cover"> 

要清楚,我需要:

https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg 
+0

(HTTP:// htmlparsing .com/php.html) – Rahul

+0

我應該特別檢查哪些答案題? –

+0

它的標題是_如何正確解析HTML,沒有正則表達式_。一個有用的指針也許。 – Rahul

回答

3

沒有正則表達式:[檢查了這一點]

$dom = new DomDocument; 
$dom->loadHTML(' 
<a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) no-repeat center center;background-size:cover"> 
'); 
$xpath = new DomXpath($dom); 
echo $xpath->evaluate('substring-before(substring-after(string(//a/@style), "background:url("), ")")'); 

Demo

+0

正如你所說,它很慢,但它確實正確地回答了這個問題。 TKS! –

+1

別提了:) – splash58