Jump to content

[SOLVED] Help about matching iframes


katerina

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
    }
}

Link to comment
Share on other sites

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.

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.