ashii Posted March 11, 2011 Share Posted March 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/230311-issue-in-simplexmlelement/ Share on other sites More sharing options...
Tonic-_- Posted March 11, 2011 Share Posted March 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230311-issue-in-simplexmlelement/#findComment-1186063 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.