sniperscope Posted January 6, 2012 Share Posted January 6, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/254462-grab-certain-tags/ Share on other sites More sharing options...
scootstah Posted January 6, 2012 Share Posted January 6, 2012 Seems to work: /^<div id="power-push2">(.*)<\/div>$/i Quote Link to comment https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304735 Share on other sites More sharing options...
ragax Posted January 6, 2012 Share Posted January 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304743 Share on other sites More sharing options...
sniperscope Posted January 6, 2012 Author Share Posted January 6, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304758 Share on other sites More sharing options...
abareplace Posted January 6, 2012 Share Posted January 6, 2012 Do you need exactly the third div with this id? Quote Link to comment https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304762 Share on other sites More sharing options...
scootstah Posted January 6, 2012 Share Posted January 6, 2012 $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? Quote Link to comment https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304763 Share on other sites More sharing options...
ragax Posted January 6, 2012 Share Posted January 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304768 Share on other sites More sharing options...
sniperscope Posted January 6, 2012 Author Share Posted January 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304771 Share on other sites More sharing options...
abareplace Posted January 6, 2012 Share Posted January 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304777 Share on other sites More sharing options...
ragax Posted January 6, 2012 Share Posted January 6, 2012 Thanks for pattern. It works well. Sweet, glad to hear it. Wishing you a fun weekend. Quote Link to comment https://forums.phpfreaks.com/topic/254462-grab-certain-tags/#findComment-1304836 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.