Get span value with PHP

0

Where "VALUE" is the result I need to get and show

<span class="nomeClass" title="titulo" tabindex="0" aria-label="number">VALOR </span>
$el = $dom->getElementsByTagName('span');

I stopped in this part and I do not know what to do.

    
asked by anonymous 16.07.2018 / 18:36

4 answers

2
  

Your question is very broad because we know nothing about the source code of the page that we should extract the text of a certain tag span whose class is nomeClass .

     

There may be many span tags with class nomeClass and even, - why not - other elements with the same nomeClass class, see for example the following HTML code:

example.html

<!--Suposto Código fonte da pagina -->

<html>

<head>
<title>Página</title>
</head>

<body>

    <div class="nomeClass">div 1</div>

    <span class="nomeClass"> Previous</span>

    <br>

    <input type="text" class="nomeClass">

    <br>

    <span class="nomeClass">Complete HTML </span>

    <br>

    <span class="nomeClass">Next </span>

    <div class="nomeClass">div 2</div>

    <span class="nomeClass"> Previous</span>

    <br>
    <span class="nomeClass">Complete HTML </span>

    <p>
    <span class="nomeClass" title="titulo" tabindex="0" aria-label="number">VALOR</span> 
    </p>
    <p>
    <span class="nomeClass" title="titulo" tabindex="0" aria-label="number">Esse VALOR Não interessa</span> 
    </p>
    <p>
    <span class="nomeClass" title="titulo" tabindex="0" aria-label="number">Outro VALOR Não interessa</span> 
    </p>
</body>

</html>
  

Note that the text of the element we want to get is the index element 8 , ie the ninth element whose class is nomeClass , regardless of whether it is a span tag.

Assuming the HTML above we can do as follows:

loemexample.php

    <?php
    $html = file_get_contents("exemplo.html");
    $DOM = new DOMDocument();
    @$DOM->loadHTML($html);
    $finder = new DomXPath($DOM);

    $classname = 'nomeClass';

    $nodes = $finder->query("//*[contains(@class, '$classname')]");
    foreach ($nodes as $node) {
      $result=$result.$node->nodeValue.",";
    }

    $partes = explode(',',$result);
    $textoSpan=$partes[8];

    echo $textoSpan;

    ?>

above example running on server

  

Anyway, even if there is only one element with class nomeClass the above code will work perfectly. Note that in this case the element index must be 0 (zero)

    
16.07.2018 / 21:54
1

Paul can put an id in span to get the value of it.

<!--No HTML-->
<span id="mySpan">Conteúdo do meu span</span>
//No javascript
document.getElementById("mySpan").innerHTML;

To get with class element

var elementoUm = document.getElementsByClassName('mySpan')[0].innerHTML; 

In this case the document.getElementsByClassName will get all elements with the class mySpan and in [0] I'm getting the first element of the page with class mySpan.

I hope I have helped.

    
16.07.2018 / 18:42
1

If your problem is PHP, solve it with PHP - I did not understand why other responses used JavaScript.

You started well. In fact, the solution will be using the DOMDocument class, however, to search the element in the tree, you'd better use the class DOMXPath

$html = <<<HTML
    <span class="nomeClass" title="titulo" tabindex="0" aria-label="number">VALOR</span>
HTML;

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

$xpath = new DOMXPath($dom);

foreach($xpath->query('//span[@class="nomeClass"]') as $span) {
    echo $span->nodeValue, PHP_EOL;
}

See working at Repl.it

Note that the DOMXPath object was instantiated in DOMDocument and executed the //span[@class="nomeClass"] search, which will search for all span elements that have the class equal to nomeClass . Since the return can be an array , it is necessary to iterate over the result with foreach , obtaining the value of the element with nodeValue .

For the above example, the output will be VALOR .

    
16.07.2018 / 20:51
-1

I think with jquery you can get what you need. var valor = $('.nomeClass').text(); alert(valor);// VALOR

Remember that the same class is often used for multiple elements on the same html page. prefer to use Id , for example.

    
16.07.2018 / 21:09