Jump to content

PREG_MATCH: how to grab from <div class ?


cedric

Recommended Posts

Hi all Guru,

 

I'm new to preg_match and i'm struggling for to grab the word “Title: Toy Car" from below sample source.

 

My intention is to grab item title and the price from a site and display it in my site after user put in the link.

And also can I use multiple preg_match for grab some others detail from the site in single page?

 

First Source:

<div class="tb-detail-hd">
<h3>Title: Toy Car</h3>
<p>
   </p><div class="tb-report">
<p class="tb-report-heading">Report<span class="tb-arrow"></span></p>
  ...someother coding
</div>
     <p></p>
</div>

 

Second source:

<strong id="J_StrPrice">$150.00</strong>

 

my coding:

$page_contents = file_get_contents("###");
preg_match('~<div class="tb-detail-hd"><h3>(.*?)</h3>~i',$page_contents, $matches);
print_r($matches[0]);

 

but empty result... :confused:

 

Please help me to sort this out...millions thanks ya! Orz

 

Link to comment
https://forums.phpfreaks.com/topic/251819-preg_match-how-to-grab-from/
Share on other sites

There's a newline between <div class="tb-detail-hd"> and <h3>

 

The \s short-hand character class will match spaces, tabs, and new lines. Use that, and match between 0 and unlimited times.

 

Hi xyph,

 

Thanks for your solution.

I have finally get an output for the title.

However, when I try for second source <strong id="J_StrPrice">$150.00</strong>, I got no result returned.

preg_match('~<strong id="J_StrPrice">(.*?)</strong>~i',$page_contents, $matches);

 

Anything wrong with my coding?

 

And how I can use multiple preg_match in one page to grab several data? If is something like below?

$page_contents = file_get_contents("####url");
preg_match('~<div class="tb-detail-hd">\s<h3>\s(.*?)</h3>~i',$page_contents, $matches);
print_r($matches[0]);

preg_match('~<strong id="J_StrPrice">(.*?)</strong>~i',$page_contents, $matches1);
print_r($matches1[0]);

 

 

 

Use DOM. Regex is not meant for HTML parsing...

 

Hi Silkfire,

 

I have just tested by using DOM.

Apparently, it have timeout problem or other issue that stop it whenever I grab data from china website.

If local sites, then it able to run.

 

everytime i run, it will get

500 Internal Server Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

XXXXXXXXXXXXXXX

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

 

Any where in this forum I can start a thread to post my question about DOM?

And I need to know where to check the timeout setting.

Archived

This topic is now archived and is closed to further replies.

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