Jump to content

Confusion with preg_match for Specific DIV can anyone help me?


lewiskk

Recommended Posts

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

Link to comment
Share on other sites

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 by BagoZonde
Link to comment
Share on other sites

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 by BagoZonde
Link to comment
Share on other sites

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.