找出正确的preg_match正则表达式匹配

时间:2013-12-16 03:36:31

标签: php regex

我正在使用php卷曲页面,然后我希望在该页面中找到一个部分。该部分使用html5 <section>标记打开和关闭,如下所示:

<section id="postingbody">
   blah blah blah content
</section>

我不确定如何使我的匹配正常工作。只是在这里填写匹配部分:

preg_match("/ id=\"postingbody\"\">???????<\/section>/i", $compiled_results, $matches2);

修改

所以这是内容的示例部分

<section id="postingbody">
    Looking to find a side job ( working your own hours ) or career in the new media field & internet marketing? Web design, graphic design, SEO, Printing & Internet marketing company looking to hire a sales team member. We have 10+ years experience in the Web design & marketing field. Work your own hours, competitive commission rates, we can also train the right candidates for sales. Our office is located in New Jersey.<br>
</section>

所以这里的例子似乎不起作用。

2 个答案:

答案 0 :(得分:2)

试试这个:

preg_match("/(?s)<section id=\"postingbody\">((?:.)*?)<\/section>/i", $compiled_results, $matches2);

Regular expression visualization

Debuggex Demo

修改例如,以下代码按预期方式工作(值在$matches2中):

$compiled_results = '<section id="postingbody">
    Looking to find a side job ( working your own hours ) or career in the new media field & internet marketing? Web design, graphic design, SEO, Printing & Internet marketing company looking to hire a sales team member. We have 10+ years experience in the Web design & marketing field. Work your own hours, competitive commission rates, we can also train the right candidates for sales. Our office is located in New Jersey.<br>
</section>';
preg_match("/(?s)<section id=\"postingbody\">((?:.)*?)<\/section>/i", $compiled_results, $matches2);
var_dump($matches2);

答案 1 :(得分:0)

正则表达式并不总是适合这种类型的HTML / XML解析。最好在PHP中使用DOM解析器。

但是,如果你真的需要,那么这个正则表达式适用于/s标志(DOTALL):

preg_match('# id="postingbody">.*?</section>#is', $compiled_results, $matches2);