Jump to content

Retrieving integers using Regex from a website HTML code


Recommended Posts

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 by Raex

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.

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.