Jump to content


Photo

Retrieving integers using Regex from a website HTML code

regex

  • Please log in to reply
2 replies to this topic

#1 Raex

Raex

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 20 August 2014 - 01:32 AM

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, 20 August 2014 - 01:38 AM.


#2 cyberRobot

cyberRobot

    Advanced Member

  • Gurus
  • 1,749 posts

Posted 20 August 2014 - 08:24 AM

Have you considered using PHP's DOMDocument class? You could use the getElementsByTagName method to get the <span> tag(s).


Blogging about PHP and other web development topics at ScorpBytes.com

#3 Jacques1

Jacques1

    Advanced Member

  • Members
  • PipPipPip
  • 955 posts

Posted 20 August 2014 - 08:35 AM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com