Take part of the html content stored in a variable

Asked

Viewed 3,032 times

1

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

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

<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 the contents within the second <p> this in PHP it is clear that 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>';

4 answers

2


It doesn’t make much sense what you want to do the way you’re doing, but I found this function on this website, that meets what you’re wanting 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 take all the content you find between the opening and closing of the informed tag. Since you only want the second one, I used the $arr[1].

See working on IDEONE.

  • is to get information from a streming

  • @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?

  • 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

  • @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.

  • 1

    Blz, Vlw helped a lot.

0

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

0

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

$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 on Repl.it.

The exit will be:

Batatinha quando nasce 

When using regex in HTML, it is interesting to read this reply.

-2

The question has become rather confusing, but I’ll try to help you, if I understand.

What you can do is create a variable and manipulate it by putting the text you want, for example potato on birth, and then insert it into the code in several ways, one of them would be the following:

(remembering that PHP, unlike HTML is a language that runs on the server, you need to have installed some server like "wamp server", have it active and the file .php must be executed 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>';
  • I don’t know how to explain it right... I want to take the contents that are inside it, not put

  • I believe that in the case just copy the contents to a string. As the content is not a variable and will not change, and who has control of the web page that is being created eh Voce, as the content is not dynamic just pick up Batatinha quando nasce and play inside the direct string, in the php:<? php $string = "Match when born";?>

  • 1

    From what I understand (the question is not at all clear), he wants to pick the text between the tag p, and not insert.

  • 2

    What he asked seems to have been the opposite of what was answered....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.