How to get the text of an HTML tag with simple_html_dom.php


I'm trying to get the text that contains certain tags within the HTML on a page, I'm using html_simple_dom.php that I thought was good for this. Without more I leave an example of my problem.

This is the function in php


require ('simple_html_dom.php');

function precioExito($url){
    $html = new simple_html_dom();
    $html->load_file($url); //Se carga la URL con la libreria
    $posts = $html->find('p[class=price offer]'); //Guardamos en Posts el precio que esta en span:price-number
    foreach($posts as $post) {  //Devuelve un array se recorre con el for para imprimir, como es unico valor, solo imprime 1
        $resultado = str_replace ( ".0", '', $post);

    return $resultado;

? >

What I do is that I call this function if I do a echo to that value, which should return to me, show me the value on the screen (see image)

But when that value that returns to me I send it to the BD, this is what keeps me in the BD. (View image)

As you can see, it's bringing me the whole HTML tag and not just the value.


How can I get only the value of this tag to be able to save it?

Thank you very much. I hope I was clear

asked by Juan Fernandez 09.01.2018 в 04:34

2 answers


If you are always going to want to get a single result you can save the foreach


$html = new simple_html_dom();
$post = $html->find('p[class=price offer]', 0)->plaintext;

With ->plaintext we extract the text. And with the second parameter in find() we indicate the number of the element to be brought.

answered by 09.01.2018 / 11:04

Your system does that because here:

$posts = $html->find('p[class=price offer]'); 

It seems you are extracting the entire label, not the value that the label contains.

Why, instead of extracting the class, do you extract the value of the id from the tag?

I do not know if what you use is a library to extract values or a function programmed by you. You do not mention it.

Here I leave something that could serve you: link


answered by 09.01.2018 в 06:38