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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.