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
https://forums.phpfreaks.com/topic/214933-regex-or-something-else/
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.

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.

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;?>

 

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.

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.

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.