6

I have some content like this

<p>some content, paragraph 1</p>
<p>some content, paragraph 2</p>
<p>some content, paragraph 3</p>

I would like to return the first paragraph i.e.

<p>some content, paragraph 1</p>

Can anyone assist me with the regex code?
'<p>(.*.)</p>' doesnt seem to work

Atif
  • 10,623
  • 20
  • 63
  • 96
  • 2
    *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Nov 24 '10 at 08:54

3 Answers3

11

you can do it this way:

if (preg_match('%(<p[^>]*>.*?</p>)%i', $subject, $regs)) {
    $result = $regs[1];
} else {
    $result = "";
}

You test your string against the regular expresion, if there are some match, you get the first and only the first one, if there are none $result will be an empty string.

If you need to get more than the first result you can iterate over the $regs array. And of you need to find any other tag change the regular expresión to macht it, for example to find IMAGE tags you use:

(<img[^>]*>.*?</img>)

EDIT: If you are processing line by line (with only the tag you are looking for) you can put ^...$ around the expresion to match a full line, like this:

if (preg_match('%^(<p[^>]*>.*?</p>)$%im', $subject, $regs)) {
    $result = $regs[1];
} else {
    $result = "";
}

HTH, Regards.

SubniC
  • 9,807
  • 4
  • 26
  • 33
  • Hi, how you can do with a "for" (loop) ? character by character ? for( $i = 0; $i <= $strlen; $i++ ) {if($cadena{$i}=="<"){NOT WORKS}} –  Mar 11 '16 at 08:08
5

Prevent from include <pre> tag, can use:

if (preg_match('/(<p(>|\s+[^>]*>).*?<\/p>)/i', $subject, $regs)) {
    $result = $regs[1];
} else {
    $result = "";
}
Jari Keinänen
  • 1,361
  • 1
  • 21
  • 43
Fantast
  • 51
  • 1
  • 2
-2
  if (preg_match("/\b1\b/i", "some content, paragraph 1")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}

Where 1 is the matched term...

Is this is any help?

benhowdle89
  • 36,900
  • 69
  • 202
  • 331