The problem for this case can be easily solved by using regular expressions.
You only need to make a small change in the target. adding class=""
to the desired tag.
<div id='central'>
<p>O Primeiro conteudo</p>
<h1>O Primeiro n sei oq</h1>
<p class="tag-alvo">Batatinha quando nasce</p>
<p>Cabooo</p>
</div>
regex would look like this: <(?:(p)).*tag-alvo.*?>(.*)<\/>
follow example: link
In php it will look like this:
$alvo = '<div id="central">
<p>O Primeiro conteudo</p>
<h1>O Primeiro n sei oq</h1>
<p class="tag-alvo">Batatinha quando nasce</p>
<p>Cabooo</p>
</div>';
$pattern = "~<(?:(p)).*tag-alvo.*?>(.*)<\/\1>~";
$resultado = preg_match_all($pattern, $alvo, $matches);
if ($resultado >= 1) {
print "achou";
var_dump($matches);
} else if ($resultado === 0) {
print "não achou".PHP_EOL;
var_dump($matches);
} else if ($resultado === false) {
print "ocorreu um erro";
}
Note that in PHP before the
in the regex I had to add another \
, here's a quote to explain why:
A character that will otherwise be interpreted as a non-escape language construct must be interpreted literally. For example, a key ({) begins the definition of a quantifier, but a backslash followed by a key ({) indicates that the regular expression engine must match the key. Similarly, a backslash marks the beginning of an escape language construct, but two backslashes (\) indicates that the regular expression engine must match the backslash.
There is a full text link to the quote:
- Character escapes in regular expressions