【问题标题】:Not use regex to parse html不使用正则表达式来解析 html
【发布时间】:2017-04-24 02:23:07
【问题描述】:

虽然answeringquestion 我发现自己在努力使用regex to parse html

我应该如何使用 html 解析器获取 style="background:url(http...)url 值?

<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

【问题讨论】:

  • 我应该在那里检查什么才能回答这个问题?
  • 标题为如何以正确的方式解析 HTML,无需正则表达式。也许是一个有用的指针。
  • 不要导致荒谬:)。这句话是在 html 搜索的上下文中。正则表达式解析style的值是相当正确的
  • @splash58 能否仅使用 html 解析器回答问题?这对我来说是一个挑战!

标签: php regex domdocument


【解决方案1】:

没有正则表达式:

$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

【讨论】:

  • 正如你所说,它很慢,但它确实正确回答了问题。 tks!
  • 别提了:)
猜你喜欢
  • 2014-06-08
  • 2014-05-16
  • 1970-01-01
  • 2014-06-26
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多