Jump to content

Grab certain tags


sniperscope

Recommended Posts

How can i get everything inside of certain tag with certain id/class name

 

Source code is:

<div id="power-push2"><a href="../?id=12" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>

 

Needed part:

<a href="../?id=12" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p>

Link to comment
https://forums.phpfreaks.com/topic/254462-grab-certain-tags/
Share on other sites

Do you mean that the ID is a variable?

 

Then run this sample code. Your ID is stored in $myID.

You can add programmatic control if you need to check multiple IDs.

I assume there won't be more that one of each ID, but just in case, this returns every instance.

 

<?php
$myID = 'power-push2';
$pattern=',<div id="'.$myID.'">(.*?)</div,';
$s=' blah <div id="power-push2"><a href="../?id=12" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div> blah';
if(preg_match_all($pattern, $s, $matches, PREG_PATTERN_ORDER)) {
$sz=count($matches[1]);
for ($i=0;$i<$sz;$i++) 
echo "Match: ".htmlentities($matches[1][$i])."<br />";
}
?>

 

Hope this helps. :)

Link to comment
https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304743
Share on other sites

Dear scootstah and playful

Thanks for your help. I really appreciated.

I want to ask last thing. What if a web page has multiple div tags with same class/id name?

 

Let's say there is 5 div tag whish has same id

<div id="power-push2"><a href="../?id=11" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=22" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=33" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=44" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=55" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>

 

 

How can i get

<div id="power-push2"><a href="../?id=33" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>

only?

Link to comment
https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304758
Share on other sites

$pattern = '/<div id="power-push2">(.*)<\/div>/i';

$str = '<div id="power-push2"><a href="../?id=11" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=22" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=33" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=44" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=55" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>';

preg_match_all($pattern, $str, $matches);

echo '<pre>'.htmlspecialchars(print_r($matches, true)).'</pre>';

 

EDIT: Oops, misread your question. Give me a minute.

 

EDIT: Is this data static or dynamic?

Link to comment
https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304763
Share on other sites

Okay, run this.

This time, both power-push2 and 33 are parameters that you feed to your regex.

You can wrap the regex in a function of $myID, $myID2.

Again, allowing for multiple instances of the {power-push2, 33} couple. (As shown when you run the code on this tweaked test string.

Otherwise we can simplify to a preg_match rather than preg_match_all..

 

<?php 
$myID = 'power-push2';
$myID2='33';
$pattern=',<div id="'.$myID.'">(<a href="\.\./\?id='.$myID2.'".*?)</div,';

$s=' blah <div id="power-push2"><a href="../?id=11" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=22" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=33" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=33" class="anotherclass"><img src="whatever, just showing you we can capture several" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=55" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div> blah';

if(preg_match_all($pattern, $s, $matches, PREG_PATTERN_ORDER)) {
$sz=count($matches[1]);
for ($i=0;$i<$sz;$i++) 
echo "Match: ".htmlentities($matches[1][$i])."<br />";
}
?>

 

Let me know if this works for you.

:)

 

ps. Hi abareplace, nice to see you here! :)

 

// Edited to add CODE tags. That's what happens when you press Submit without previewing. ;) 

Link to comment
https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304768
Share on other sites

Dear abareplace

Yes, i want third div with  id="power-push2"

 

@scootstah

Yes, data is dynamic.

All div tags, p tags or any tags store in db. In db has table which has url, start_tag and end_tag columns.

 

Get the site (cURL) from $row['url'], pars it and get data between start_tag and end_tag. This is sort of a BOT.

 

Dear Playful

Thanks for pattern. It works well.

Thanks again.

 

Have a great day everyone who tried to help me out.

Link to comment
https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304771
Share on other sites

Sniperscope, try the following code to match the third tag:

 

 

<?php

$id = 'power-push2';
$n = 3;
$pattern = '~<div id="' . $id . '">(.*?)</div>~';

$subj = ' blah <div id="power-push2"><a href="../?id=11" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=22" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=33" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=33" class="anotherclass"><img src="whatever, just showing you we can capture several" /></a><p><span class="p-w15">username</span></p></div>
<div id="power-push2"><a href="../?id=55" class="imghover"><img src="../1.jpg" alt="abc" width="160" height="213" /></a><p><span class="p-w15">username</span></p></div> blah';

if (preg_match_all($pattern, $subj, $matches, PREG_SET_ORDER) && count($matches) >= $n) {
   echo ($matches[$n - 1][1]);
}

?>

 

Hi, playful. Nice to see you, too :)

Link to comment
https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304777
Share on other sites

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.