Jump to content

regex or something else.


IwnfuM

Recommended Posts

hello.

There is a project i take and i can't handle it.

the project goes like this.

 

I get site code by file_get_contents function into variable.

Than I need to find the iframe and delete it from the variable.

for example the code i getting from the file_get_contents is looking like this :

<html>
<body>

This is site !

<iframe> LINK </iframe>

</body>
</html>

 

now , simply i can track the iframe and delete it just like this.

$str = preg_replace('/\<{1}iframe[a-zA-Z0-9=+\/\'.]/' , '' , $str);/** this is not the right regex i use , the full one contain all chars except < .**/

So I can find the < symbol and than delete all iframe section.

the problem come when I get code like this.

<html>
<body>

This is site !

<iframe> <iframe>LINK </iframe></iframe>

</body>
</html>

So , from here I can't handle it since I don't know where the iframe ends since there is many < symbols.

 

Second question is maybe with regex i can handle something like this?

 

looking for start , than get everything between and stop at the end.

$reg ='/^\<{1}iframe .+\<{1}iframe\>{1}/i';

Is that possible to do something like this with regex?

 

any suggestions how to make it done?

hope i was clear.

thanks , Mor.

Link to comment
Share on other sites

You could use something like (not tested):

 

$string = file_get_contents('somefile.php');

$search = '/<iframe(.*)\/iframe>/is';
$replace = '';

$string = preg_replace($search,$replace,$string);

 

Only problem with that is that it if you forget the ending iframe tag it will break the layout.

Link to comment
Share on other sites

There is a mistake , I took this string to check the regexp 

$string = '<td class="alt3" style="border-top:#57a6e9 1px solid; padding: 8px 8px 8px 8px" align="center"><iframe id=a746194 ame=a7461948 
src=http://site.com/www/delivery/afr.php?zoneid=239 frameborder=0 scrolling=no width=180 height=150><a 
href=http:pera.co.il/www/delivery/ck.php?n=ab67277b target=_blank><img src=http://site.com/www/delivery/avw.php?zoneid=239&n=ab67277b border=0 alt= /></a></iframe></td>';

 

and it takes all the code with the iframe , it eat all of it , not only the iframe tags.

 

I just tested it few times and there is an other problem.

if this code given by the file_get_contents

code1
<iframe id and shit> link </iframe>
code2
<iframe id and shit > link </iframe>
code3

it will take evrything until the last iframe close tage.

for example it will left only the code1 and code3.

 

hope it was clear.

thanks , Mor.

Link to comment
Share on other sites

Ok I think I have worked it out for you...try this:

 

 

<?$string = 'code1<iframe id="test"> <a href="testing">test</a> </iframe>code2<iframe id="test2" > link </iframe>code3';$search = '@(<iframe[^>]+>)@i';$replace = '';$string = preg_replace($search,$replace,$string);echo $string;?>

 

Link to comment
Share on other sites

Again it doing some problems.

I took this string :

$string = '<td class="alt3" style="border-top:#57a6e9 1px solid; padding: 8px 8px 8px 8px" align="center"><iframe id=a746194 ame=a7461948 
src=http://site.com/www/delivery/afr.php?zoneid=239 frameborder=0 scrolling=no width=180 height=150><a 
href=http:pera.co.il/www/delivery/ck.php?n=ab67277b target=_blank><img src=http://site.com/www/delivery/avw.php?zoneid=239&n=ab67277b border=0 alt= /></a></iframe></td>';

 

and regex it , it remove everything until the <a href...

and it left in the string the :

<a 
href=http:pera.co.il/www/delivery/ck.php?n=ab67277b target=_blank><img src=http://site.com/www/delivery/avw.php?zoneid=239&n=ab67277b border=0 alt= /></a></iframe></td>

part.

 

this regex is almost perfect , only little fix needed I think.

 

and thanks , it's great.

- Mor.

Link to comment
Share on other sites

Sorry I think I misunderstood. I thought you wanted anything between the iframe tags left as well. I found this function on php.net in the comments for strip_tags http://us3.php.net/manual/en/function.strip-tags.php:

 

function strip_only($str, $tags, $stripContent = false) {
    $content = '';
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) {
        if ($stripContent)
             $content = '(.+</'.$tag.'[^>]*>|)';
         $str = preg_replace('#</?'.$tag.'[^>]*>'.$content.'#is', '', $str);
    }
    return $str;
} 

 

Example use:

$string = strip_only($string,array('iframe'),true);
echo $string;

 

If you want the content between the tag to remain just change the last argument to false.

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.