通过正则表达式提取数据

时间:2012-05-03 16:14:31

标签: php preg-match

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

<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];

2 个答案:

答案 0 :(得分:1)

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

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

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

return $matches[0];

答案 1 :(得分: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解析器会将它们全部考虑在内。