IwnfuM Posted October 1, 2010 Share Posted October 1, 2010 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. Quote Link to comment Share on other sites More sharing options...
petroz Posted October 1, 2010 Share Posted October 1, 2010 You could use strip_tags.. <?php $site = file_get_contents("http://localhost:8888/iframe.php"); //replace the url with yours... $no_iframe = strip_tags($site,"<p><h2>"); print_r($no_iframe); Quote Link to comment Share on other sites More sharing options...
IwnfuM Posted October 1, 2010 Author Share Posted October 1, 2010 There is one problem. I need to get off the iframe index out too , what inside the iframe. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 1, 2010 Share Posted October 1, 2010 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. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted October 1, 2010 Share Posted October 1, 2010 If the regexp contains slash characters I find it more convenient to use a different regexp delimiter: $search = '@<iframe(.*)</iframe>@is'; Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 1, 2010 Share Posted October 1, 2010 Thanks roopurt18....I am fairly new to regexp myself and didnt know you could use the @ symbol as a delimiter, I just escape them using the back slash but that does make it easier to manage. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted October 1, 2010 Share Posted October 1, 2010 Also keep in mind that backslash in single quotes is special and you'd really need two of them to send it to the regexp engine correctly: $regexp = '/<iframe(.*)\\/iframe>/is'; // Ugly! Quote Link to comment Share on other sites More sharing options...
IwnfuM Posted October 1, 2010 Author Share Posted October 1, 2010 that is so epic. Lemme understand that , the /<iframe(.*) means find iframe tags , one or common (or zero). and than I haven understand this part , what this \\ part does? thanks lot , Mor. Quote Link to comment Share on other sites More sharing options...
IwnfuM Posted October 1, 2010 Author Share Posted October 1, 2010 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. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 2, 2010 Share Posted October 2, 2010 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;?> Quote Link to comment Share on other sites More sharing options...
IwnfuM Posted October 2, 2010 Author Share Posted October 2, 2010 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. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 2, 2010 Share Posted October 2, 2010 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. 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.