Take content from a div and save to a regular expression array (PHP)

Asked

Viewed 757 times

1

I wonder how I can take the contents of the div with class="x-product" and save in an array so I can manipulate them later.

I tried that code but it didn’t 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 then take the image, the description and the value of the product to put in a page of the site I am creating.

  • Didn’t work means he didn’t pick up the pattern or came over things?

1 answer

0


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

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

  • Thanks! I got it with a manual code you suggested.

Browser other questions tagged

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