lewiskk Posted January 23, 2013 Share Posted January 23, 2013 Can anyone tell me how to fetch all data in specific DIV and inside it is specifec Tag " <p></p>" of given url I Tried this but changing tag names but i Failed please any php expert can me help me ? <?php function getdata($Url){ $str = file_get_contents($Url); if(strlen($str)>0){ preg_match("/\<title\>(.*)\<\/title\>/",$str,$data); return $data[1]; } } echo getdata("http://www.w3schools.com/"); ?> Example URL : www.productsite.com Product Web HTML Example : <div class="products"> <a href="productsite.com" class="title"><strong>Product 1</strong></a><br/> <p>http://www.productsi...1.aspx<br/></p> <br/> </div> <div class="products"> <a href="productsite.com" class="title"><strong>Product 2</strong></a><br/> <p>http://www.productsi...2.aspx<br/></p> <br/> </div> <div class="products"> <a href="productsite.com" class="title"><strong>Product 3</strong></a><br/> <p>http://www.productsi...3.aspx<br/></p> <br/> </div> <div class="products"> <a href="productsite.com" class="title"><strong>Product 4</strong></a><br/> <p>http://www.productsi...4.aspx<br/></p> <br/> </div> <div class="products"> <a href="productsite.com" class="title"><strong>Product 5</strong></a><br/> <p>http://www.productsi...5.aspx<br/></p> <br/> </div> <div class="products"> <a href="productsite.com" class="title"><strong>Product 6</strong></a><br/> <p>http://www.productsi...6.aspx<br/></p> <br/> </div> <div class="products"> <a href="productsite.com" class="title"><strong>Product 7</strong></a><br/> <p>http://www.productsi...7.aspx<br/></p> <br/> </div> and so on ... This Output i need : http://www.productsi...ducatpage1.aspx http://www.productsi...ducatpage2.aspx http://www.productsi...ducatpage3.aspx http://www.productsi...ducatpage4.aspx http://www.productsi...ducatpage5.aspx http://www.productsi...ducatpage6.aspx http://www.productsi...ducatpage7.aspx and so on ... Quote Link to comment https://forums.phpfreaks.com/topic/273518-confusion-with-preg_match-for-specific-div-can-anyone-help-me/ Share on other sites More sharing options...
BagoZonde Posted January 23, 2013 Share Posted January 23, 2013 (edited) You're looking for that: function getStringFromHTML($string) { $pattern = "/<p>(.*)<\/p>/"; preg_match_all($pattern, $string, $matches); return $matches[1]; } $output = getStringFromHTML($html); print '<pre>'; print_r($output); print '</pre>'; Edited January 23, 2013 by BagoZonde Quote Link to comment https://forums.phpfreaks.com/topic/273518-confusion-with-preg_match-for-specific-div-can-anyone-help-me/#findComment-1407649 Share on other sites More sharing options...
BagoZonde Posted January 23, 2013 Share Posted January 23, 2013 (edited) I've played a little more with regex as I like it so here's the code where you can specify tag, additionaly if this tag has classes it will still work, i.e. <p class="something" id="something_something"> function getStringForTag($string, $tag){ $pattern = "/<".$tag."[^>]*>(.*)<\/".$tag.">/"; preg_match_all($pattern, $string, $matches); return $matches[1]; } $output = getStringForTag($html, 'p'); Edited January 23, 2013 by BagoZonde Quote Link to comment https://forums.phpfreaks.com/topic/273518-confusion-with-preg_match-for-specific-div-can-anyone-help-me/#findComment-1407653 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.