cedric Posted November 26, 2011 Share Posted November 26, 2011 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... Please help me to sort this out...millions thanks ya! Orz Quote Link to comment Share on other sites More sharing options...
xyph Posted November 26, 2011 Share Posted November 26, 2011 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. Quote Link to comment Share on other sites More sharing options...
cedric Posted November 27, 2011 Author Share Posted November 27, 2011 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]); Quote Link to comment Share on other sites More sharing options...
silkfire Posted November 27, 2011 Share Posted November 27, 2011 Use DOM. Regex is not meant for HTML parsing... Quote Link to comment Share on other sites More sharing options...
cedric Posted November 27, 2011 Author Share Posted November 27, 2011 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. Quote Link to comment 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.