0
<?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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
cj333
  • 2,547
  • 20
  • 67
  • 110

2 Answers2

4

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(); }
mario
  • 144,265
  • 20
  • 237
  • 291
2

(.*?) 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);
?>
Canuteson
  • 598
  • 1
  • 4
  • 11