0

我想提取以下形式的数据:

<div class="image"><a href="[Any Chacter]">  

我得到了数据,<div class="image">但在那之后没有结果。这是我的代码:

$tag_regex='/<div class="image">/';
preg_match_all($tag_regex,$xml,$matches);

return $matches[0];
4

2 回答 2

1

正如 Truth 在他的评论中所说,从 html 中提取数据的正确方法是使用 html 解析器。

但是,您的情况很简单,可以使用正则表达式轻松快速地解决:

$tag_regex= '<div class="image"><a href=".*">';
preg_match_all($tag_regex,$xml,$matches);

return $matches[0];
于 2012-05-03T16:32:37.607 回答
0

我很高兴你愿意学习,我真的希望你能学会使用 HTML 解析器(就像任何理智的人一样)。

对于您的问题的实际解决方案:

$tag_regex= '|<div class="image"><a href="(.*)">|i';
preg_match_all($tag_regex,$xml,$matches);

return $matches[1]; //Will match what's in the first set of brackets, I.e the href.

请注意,此模式并不稳健。它不考虑空格、不同种类的引号、换行符和许多其他东西。HTML 解析器将解决所有这些问题。

于 2012-05-03T16:48:51.647 回答