php preg match all, all the `p`
<?php
$str= <<<ETO
<p>one
two</p>
<p>three</p>
ETO;
preg_match_all('/<p>(.*?)<\/p>/',$str,$r);
print_r($r);
?&开发者_如何学运维gt;
I am studying preg_match_all
. I want get all the p
from one article. but my code only get the second p
. how to modify so that I can get the first p
, either. Thanks.
You are missing the /ims
flag at the end of your regex. Otherwise .
will not match line breaks (as in your first paragraph). Actually /s
would suffice, but I'm always using all three for simplicity.
Also, preg_match works for many simple cases. But if you are attempting any more complex extractions, then consider alternating to phpQuery or QueryPath which allow for:
foreach (qp($html)->find("p") as $p) { print $p->text(); }
(.*?) is not matching newline characters. Try the /s modifier:
<?php
$str= <<<ETO
<p>one
two</p>
<p>three</p>
ETO;
preg_match_all('/<p>(.*?)<\/p>/s',$str,$r);
print_r($r);
?>
精彩评论