katerina Posted October 4, 2008 Share Posted October 4, 2008 Hi, I want to match all <iframe> ements. To be more specific , let the text : $text='<iframe src ="/default.asp" width="100%"> </iframe><Iframe longdesc="Hello" src="a.php"></iframe> <iframe src="b.php" longdesc="Hi">Hello world</iframe>'; I want to check $text and print : I found 3 <iframe> elements, but only 1 <iframe> of them, has both longdesc attibute and text between <iframe></iframe>. Could somebody help me ? Thanks Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 4, 2008 Share Posted October 4, 2008 Here is my shot at it: $data = <<<DATA text='<iframe src ="/default.asp" width="100%"> </iframe><Iframe longdesc="Hello" src="a.php"></iframe> <iframe src="b.php" longdesc="Hi">Hello world</iframe>'; DATA; $dataSplit = preg_split('#</iframe>#i', $data); array_pop($dataSplit); // delete last key in array... echo 'I found '. count($dataSplit) . ' < iframe > elements'; $attributeAndTextCount = 0; foreach($dataSplit as $val){ if(preg_match('#[^<]*<iframe([^>]+)>([^\r\n\s]+)#i', $val, $match)){ $attributeAndTextCount++; } } if($attributeAndTextCount){ echo ' and ' . $attributeAndTextCount . ' of them has both the longdesc attibute and text between < iframe >< /iframe >.'; } else { echo '.'; } Ouputs: I found 3 < iframe > elements and 1 of them has both the longdesc attibute and text between < iframe >< /iframe >. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 4, 2008 Share Posted October 4, 2008 Ok, I think in my last attempt, there were some issues.. I have made some refinements: $data = <<<DATA text='<iframe src ="/default.asp" width="100%"> </iframe><Iframe longdesc="Hello" src="a.php"></iframe> <iframe src="b.php" longdesc="Hi">Hello world</iframe>'; DATA; // find all matching <iframe> tags... preg_match_all('#(<iframe[^>]*>[^<]*</iframe>)#i', $data, $matches); echo 'I found '. count($matches[0]) . ' matching < iframe > elements'; $attributeAndTextCount = 0; foreach($matches[0] as $val){ if(preg_match('#[^<]*<iframe.+?longdesc="[^"]+"(?:[^>]+)?>.+</iframe>#i', $val, $v)){ $attributeAndTextCount++; } } if($attributeAndTextCount){ echo ' and ' . $attributeAndTextCount . ' of them has both the longdesc attibute and text between < iframe >< /iframe >.'; } else { echo '.'; } This solution should work out much better than the last one. Cheers, NRG Quote Link to comment Share on other sites More sharing options...
katerina Posted October 5, 2008 Author Share Posted October 5, 2008 Thank you very very much for your code! !Just perfect! Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 5, 2008 Share Posted October 5, 2008 No problem. Looking back at the preg regex inside the foreach loop for $matches[0], I still had some remnant code in the first half of the pattern from the first iteration I did.. It doesn't effect the outcome granted. That preg could be replaced with: if(preg_match('#.+?longdesc="[^"]+"(?:[^>]+)?>.+</iframe>#i', $val, $v)){ // I eliminated the '[^<]*<iframe' part from the beginning. Quote Link to comment Share on other sites More sharing options...
ghostdog74 Posted October 6, 2008 Share Posted October 6, 2008 I want to check $text and print : I found 3 <iframe> elements, but only 1 <iframe> of them, has both longdesc attibute and text between <iframe></iframe>. Could somebody help me ? Thanks $text='<iframe src ="/default.asp" width="100%"> </iframe><Iframe longdesc="Hello" src="a.php"></iframe><iframe src="b.php" longdesc="Hi">Hello world</iframe>'; $iframes = split("</iframe>",$text); foreach( $iframes as $k=>$v){ if ( strpos($v,"<Iframe longdesc") !==FALSE){ echo "Found"; } } Quote Link to comment Share on other sites More sharing options...
katerina Posted October 6, 2008 Author Share Posted October 6, 2008 The code does not work for site http://www.contra.gr Maybe because between <iframe> and </iframe > does not exist text And in the first regular expression you use [^<]* Is there another solution ? Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 6, 2008 Share Posted October 6, 2008 The code does not work for site http://www.contra.gr ... Is there another solution ? Ah.. ok.. This is a newer version.. it should hopefully solve things: // find all matching <iframe> tags... preg_match_all('#(<iframe[^>]*>.*?</iframe>)#si', $data, $matches); echo 'I found '. count($matches[0]) . ' matching < iframe > elements'; $attributeAndTextCount = 0; foreach($matches[0] as $val){ if(preg_match('#longdesc="[^"]+"#i', $val)){ $attributeAndTextCount++; } } if($attributeAndTextCount){ echo ' and ' . $attributeAndTextCount . ' of them has both the longdesc attibute and text between < iframe >< /iframe >.'; } else { echo '.'; } It is now even more refined and efficient than before. Please let me know if that new pattern works out or not. Cheers, NRG P.S Maybe because between <iframe> and </iframe > does not exist text And in the first regular expression you use [^<]* A handy way to find out.. is to right-click on the site, select 'View Page Source' (or something to that effect.. the wording depends on the browser), and it should open the source code in a new tab. Go to that new tab, then hit F3 (this should be relatively universal across major up-to-date browsers).. this brings up a seach field of some sort..(it may be built into the interface somewhere as opposed to a floating window) type in '<iframe>', and if there is any instances that exist, the browser should highlight those keywords in the source code.. there is usually some form of 'next' or previous' that you can use to jump to multiple instances of this keyword.. hope this helps out.. so yes, http://www.contra.gr contains <iframe> tags. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 6, 2008 Share Posted October 6, 2008 In my last post, be sure to pay attention to (and use) this line: if(preg_match('#longdesc="[^"]+"#i', $val)){ as well as the new pattern, as this has been revamped to be more simplified and effective. Cheers, NRG 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.