Catch part of the html content stored in a variable

1

I have a problem that I could not find a solution on the net.

Simple I want to get only the second <p> of text, of div in case, it is + - like this:

<div id='central'>
<p>O Primeiro conteudo</p>
<h1>O Primeiro n sei oq</h1>
<p>Batatinha quando nasce</p>
<p>Cabooo</p>
</div>

In case I want to get content within the second <p> this in PHP of course the content would be in a string to be manipulated.

$string = '   <div id="central">
    <p>O Primeiro conteudo</p>
    <h1>O Primeiro n sei oq</h1>
    <p>Batatinha quando nasce</p>
    <p>Cabooo</p>
    </div>';
    
asked by anonymous 22.02.2016 / 01:42

4 answers

2

It does not make much sense what you want to do the way you're doing, but I found this on this site , which meets what you want to do:

function get_tag($txt,$tag){
    $offset = 0;
    $start_tag = "<".$tag;
    $end_tag = "</".$tag.">";
    $arr = array();
    do{
        $pos = strpos($txt,$start_tag,$offset); 
        if($pos){
            $str_pos = strpos($txt,">",$pos)+1;
            $end_pos = strpos($txt,$end_tag,$str_pos);  
            $len = $end_pos - $str_pos;
            $f_text = substr($txt,$str_pos,$len);


    $arr[] = $f_text;
        $offset = $end_pos;
    }
}while($pos);
return $arr;

}

$txt = '<div id="central">
<p>O Primeiro conteudo</p>
<h1>O Primeiro n sei oq</h1>
<p>Batatinha quando nasce</p>
<p>Cabooo</p>
</div>';
$arr = get_tag($txt, "p");

echo $arr[1]; 

In this case, the code will pick up all the content it finds between opening and closing the tag. Since you only want the second, I used $arr[1] .

See working at IDEONE .

    
22.02.2016 / 02:43
0

The best way to treat HTML with PHP is to use the DOMDocument class:

$string = '   <div id="central">
    <p>O Primeiro conteudo</p>
    <h1>O Primeiro n sei oq</h1>
    <p>Batatinha quando nasce</p>
    <p>Cabooo</p>
    </div>';

$dom = new DOMDocument();
$dom->loadHTML($string);

$div = $dom->getElementById("central");
$ps = $div->getElementsByTagName("p");

echo $ps[1]->nodeValue;
  

See working at Repl.it .

The output will be:

Batatinha quando nasce 

How much to use regex in HTML, it is interesting to read this response .

    
20.06.2017 / 18:38
0

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

    
20.06.2017 / 18:26
-2

The question got pretty confusing, but I'll try to help you if I understand.

What you have to do is to create a variable and manipulate it, putting whatever text you want, for example potato when it is born , and then insert it into the code in several ways, one of them would be the following:

(remember that PHP, other than HTML, is a language that runs on the server, you need to have some server installed as the "wamp server", have it active and the .php file must be run on the server)

<?php
    $string ="Batatinha quando nasce";
?>
    <div id="central">
    <p>O Primeiro conteudo</p>
    <h1>O Primeiro n sei oq</h1>
<?php
    echo'<p>'.'$string'.'</p>';
?>
    <p>Cabooo</p>
    </div>';
    
22.02.2016 / 02:07