Take <span> value with PHP

Asked

Viewed 1,386 times

0

Where is "VALUE" is the result I need to pick up and show

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

I stopped at that part and I don’t know what to do.

  • Hello @Paulo Previatto, You want to get the value of an input, this?

  • sorry, the site does not show the whole code, I updated the post , take a look again

  • updated, then it is the value of an html element, in this case a span. You are aware in jquery?

  • little acquaintance friend

  • vc already got the value from the database with php and now would like to read with js/jquery to be able to manipulate it in other situations of your application, this?

  • I’m pulling a page out, so I don’t pull the whole page and get loaded with content, I wanted to take only this VALUE that is on the page within this span

  • Paulo with or without jquery if span does not have an Idm you need to find another way to find the element on the page, you need the page address or the HTML code to create a query

  • Thank you so much for your help Neo, I managed to solve the problem !

Show 3 more comments

4 answers

2


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

There may be numerous tags span with class nomeClass and even, - why not - other elements with the same class nomeClass, 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, is the ninth item whose class is nomeClass, regardless of being a tag span.

Assuming the HTML above we can do as follows:

loadexemplo.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;

    ?>

example above running on the server

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

  • thanks a lot for the help friend, I managed to solve the problem!

1

If your problem is PHP, solve it with PHP - I don’t understand why other answers used Javascript.

You started well. In fact, the solution will be using the class DOMDocument, however, to search for the element in the tree, it will be better if you 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 on Repl.it

Notice that the object has been instantiated DOMXPath in relation to the DOMDocument and executed the search //span[@class="nomeClass"], that will seek all elements span which have the class equal to nomeClass. How the return can be one array, it is necessary to iterate on the result with the foreach, obtaining the value of the element with nodeValue.

For the above example, the output will be VALOR.

  • thanks a lot for the help friend, I managed to solve the problem!

1

Paul you 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 pick with class element.

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

In this case Document.getElementsByClassName will take all elements with the mySpan class and in [0] I am taking the first element of the page with the mySpan class.

I hope I’ve helped.

  • I’m pulling content from another page, and the code has no ID to span

  • instead of pulling the id of the span, could not pull type through the class or title ??

  • I added the example with class in the reply, because as a comment it is difficult to visualize.

  • yes with this script I can pick up the line, and to show how I do ?

  • In this case you just play the elementA variable where you want it. For example if you want the content to go to the div test it would be. Document.getElementById("test"). innerHTML = elementA;

  • Thank you Roberto, I managed to solve the problem !

Show 1 more comment

0

believe q with jquery you can get the q needs. var valor = $('.nomeClass').text(); alert(valor);// VALOR

lember-if q usually uses the same class for several elements on the same html page. prefer to use the Id, for example.

  • 2

    I voted against for two reasons: 1) the question is about PHP, not Javascript; 2) because even if it were Javascript, using jQuery for just that would be completely unnecessary.

  • not always what you want and how you want to do it are not the best options. comments can help or not.

  • I’d be ashamed to ask a question like that with a resume like yours. Good luck!

  • Thank you, problem has been solved !

Browser other questions tagged

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