How to get the values inside multiple tags

1

Hello,

I have the following HTML page:

<!DOCTYPE html>
<html>
<head>
    <title>Exemplo</title>
</head>
<body>
    <div id="text">Valor 1</div>
    <div id="text">Valor 2</div>
    <div id="text">Valor 3</div>
</body>
</html>

I'm using the following function in PHP to get the text between a tag:

function capturar($string, $start, $end) {
    $str = explode($start, $string);
    $str = explode($end, $str[1]);
    return $str[0];
}

Example usage:

<?php
$url = file_get_contents('http://localhost/exemplo.html');
$valor = capturar($url, '<div id="text">', '</div>');
echo $valor;

But when I have more than one identical tag with different text between them, it only takes the text between the first tag, what I would do to get all the texts between that tag ( <div id="text"> , </div> ) and to add them to an array?

Thank you in advance.

    
asked by anonymous 01.03.2017 / 09:47

2 answers

-1

The ideal is to use preg_match_all.

function capturar($string, $start, $end) {

    $start = str_replace('/', '\/', $start);
    $end = str_replace('/', '\/', $end);
    preg_match_all('/'.$start.'(.*?)'.$end.'/', $string, $matches);
    if(isset($matches[1]))
        return $matches[1];
    else return false;
}

It may not be perfect, as you may need to "escape" other characters. Now just do the /

    
01.03.2017 / 11:02
-1

There are several ways to search for elements in an HTML document. First you should note that your HTML is invalid, as the id attribute must be unique throughout the document.

You can use XPath queries to resolve your issue:

$html = <<<'HTML'
<!DOCTYPE html>
<html>
<head>
    <title>Exemplo</title>
</head>
<body>
    <div id="text">Valor 1</div>
    <div id="text">Valor 2</div>
    <div id="text">Valor 3</div>
</body>
</html>
HTML;

$crawler = new DomDocument();
$crawler->loadHTML($html);
$xpath = new DOMXPath($crawler);

$elementos = $xpath->query("//div[@id='text']");

echo '<pre>';
foreach($elementos as $objeto) {
    var_dump($objeto->nodeValue);
}

Another slightly easier way and one that gives you more resources is through third-party libraries. A very powerful and useful library at DomCrawler .

To install using composer:

composer require symfony/dom-crawler

Same solution as before, using DomCrawler :

require __DIR__ . '/vendor/autoload.php';

use Symfony\Component\DomCrawler\Crawler;

$html = <<<'HTML'
<!DOCTYPE html>
<html>
<head>
    <title>Exemplo</title>
</head>
<body>
    <div id="text">Valor 1</div>
    <div id="text">Valor 2</div>
    <div id="text">Valor 3</div>
</body>
</html>
HTML;

$crawler = new Crawler($html);

$elementos = $crawler->filterXPath("//div[@id='text']")->extract(['_text']);

echo '<pre>';
foreach($elementos as $elemento) {
   var_dump($elemento);
}

Note that in addition to this example, the official documentation still indicates that it is possible to use CSS selectors, including the symfony/css-selector dependency.

    
01.03.2017 / 15:59