2017-04-23 31 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