The problem for this case can be easily solved using regular expressions.
You only need to make a small target change. by adding a class=""
in 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>
the regex would look like this: <(?:(p)).*tag-alvo.*?>(.*)<\/\1>
following example: https://regex101.com/r/pRqtE/1
In php 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 \1
in regex I had to add another \
, follows quote to explain the reason:
A character that otherwise will be interpreted as a language construct without escape 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 mechanism must match the key. Similarly, a backslash marks the beginning of an escape language construction, but two backslash ( ) indicates that the regular expression mechanism must match the backslash.
Follow link with full text about the citation:
- Character escapes in regular expressions
is to get information from a streming
– Leandro Ferreira
@Leandroferreira on the question, you say you want to get the text inside the tag
p
, in a codeword that is in a variable in php. The answer answers the question, but what is streming?– user28595
Streaming is like a browser communication system it has various information like frequency who is communicating these things - an example of http straming://158.69.17.192:9992 dai has some pages of this type q I have to get the information so it returns a string only with a lot of 'p' and I want to get only the second one
– Leandro Ferreira
@Leandroferreira ah, since you already have the variable filled with the content, as you showed in the code, this function will fit perfectly. Alias, not just for tag
p
, but you can use it to pick up the contents of any tag. Just be careful with equal internal tags (div type inside div), because in this case the function may display an unexpected behavior.– user28595
Blz, Vlw helped a lot.
– Leandro Ferreira