Get content from a div and save it to an array with regular expressions (PHP)

1

I would like to know how I can get the contents of the div with class="x-product" and save it to an array so I can manipulate them later.

I tried this code but it did not work.

<?php

$url = "http://www.megamamute.com.br/hardware/placas-de-video?PS=16";

$conteudo=file_get_contents($url);
preg_match_all('/<div class="x-product">(.+)<\/div>/', $url, $conteudo);

//echo $conteudo[1][0];
echo $conteudo;
?>

Note: I want to get the image, the description and the value of the product to put on a page of the site I'm creating.

    
asked by anonymous 07.03.2016 / 16:31

1 answer

0

Ana trying to get content from HTML elements with regular expressions is a bad idea and at best will only work partially because HTML is not a regular language (which is the kind that regular expressions can work with).

I suggest you use an HTML parser for what you want, a good option is the DOM module , you can see here for some good examples of usage.

    
07.03.2016 / 18:06