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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…