Jump to content

Issue in SimpleXMLElement


ashii

Recommended Posts

Hello!

I am using CURL to access HTML and extract all the elements from it as below

**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,'http://www.w3schools.com/PHP/php_xml_simplexml.asp');
/**
* Ask cURL to return the contents in a variable
* instead of simply echoing them to the browser.
*/
          curl_setopt($ch, CURLOPT_FAILONERROR,1);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_TIMEOUT, 15);
/**
* Execute the cURL session
*/
$contents = curl_exec ($ch);

/**
* Close cURL session
*/
curl_close ($ch);

 

This works perfectly , but my next step is to use SimpleXMLElement() and grab a particular element from it which is.

$xml_data = new SimpleXMLElement($contents);

 

but I came across an article saying that there is no pint using SimpleXMLElement() to grab HTML Elements, Is that true?

and BTW if I need to  grab  a particular element (in the already extracted HTML using CURL) what is the function I am supposed to use ?

 

Thank you   

 

 

Link to comment
https://forums.phpfreaks.com/topic/230311-issue-in-simplexmlelement/
Share on other sites

For html content type pages and not XML formatted I use preg_match_all Don't know if that is what you're looking for.

 

preg_match_all('%<div id="blah">(.*?)</div>%', $contents, $blah);

$blah = $blah['1']['0'];

 

Will grab first occurrence.

 

It's better to do more research on it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.