cj333
cj333

Reputation: 2609

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);
?>

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.

Upvotes: 0

Views: 756

Answers (2)

Canuteson
Canuteson

Reputation: 598

(.*?) 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);
?>

Upvotes: 2

mario
mario

Reputation: 145482

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(); }

Upvotes: 4

Related Questions