Raex Posted August 20, 2014 Share Posted August 20, 2014 (edited) Hi, I'm trying to retrieve the integer value between the <span> tag from a HTML source code. HTML source code: <span> (3861822) </span> This is the php code: <!DOCTYPE html> <html> <body> <?php //use curl to get html content function getHTML($url,$timeout) { $ch = curl_init($url); // initialize curl with given url curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error return @curl_exec($ch); } $html=getHTML("http://www.alibaba.com/Products",10); preg_match("/<span>(.*)<\/span>/i", $html, $match); $title = $match[1]; echo $title; ?> </body> </html> Whenever I try to run it, this error will come out: Notice: Undefined offset: 1 in C:\xampp\htdocs\myPHP\index.php on line 19. How to correct it so that it will display all the integer value within the tag name but without the bracket? Thanks Edited August 20, 2014 by Raex Quote Link to comment https://forums.phpfreaks.com/topic/290550-retrieving-integers-using-regex-from-a-website-html-code/ Share on other sites More sharing options...
cyberRobot Posted August 20, 2014 Share Posted August 20, 2014 Have you considered using PHP's DOMDocument class? You could use the getElementsByTagName method to get the <span> tag(s). Quote Link to comment https://forums.phpfreaks.com/topic/290550-retrieving-integers-using-regex-from-a-website-html-code/#findComment-1488396 Share on other sites More sharing options...
Jacques1 Posted August 20, 2014 Share Posted August 20, 2014 Your regex searches for span elements which are limited to one line (there are none in the document). Without the s modifier, a dot doesn't match linebreaks. However, when you add this modifier, you'll run into the next bug: Now you get everything from the opening span tag to the very last closing span tag somewhere at the end of the document. You might want to read up on greedy quantifiers and non-greedy quantifiers. But to be honest, the entire approach is awful. I understand that this is just some quick-and-dirty hack. But simply fetching all span elements without attributes in the hopes that those happen to contain your numbers seems a bit too quick and dirty. Look at the HTML markup of the page: There's actually a structure. I see one big container with the id “category”, I see main categories with the class “item”, and I see subcategories with the class “sub-item”. Those subcategories contain your numbers within a few other elements. So a smarter approach than the usual regex massacre is indeed to actually parse the markup and let the structure guide you to the data. Quote Link to comment https://forums.phpfreaks.com/topic/290550-retrieving-integers-using-regex-from-a-website-html-code/#findComment-1488397 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.