newbtophp Posted October 9, 2009 Share Posted October 9, 2009 Example: //$file is the variable to remove the uneeded tags $file = '?><br class="clear" /> </div> <div id="footer-wrap"> <div id="footer"> <div class="span-3 append-1 small"> <?php if ( !function_exists(\'dynamic_sidebar\') || !dynamic_sidebar(\'Bottom-Left\') ) : ?> <?php endif; ?> </div><?'; //remove the uneded php tags $file = preg_replace('remove unclosed/uneded tags', ' ', $file); highlight_string($file); Im trying to figure out how to remove uneeded tags (short and full), which are unclosed or opened for no reason as shown in the above example. Edit heres some more typical examples: <?php header("Location: install.php"); ?> '; ?> ?><?php function iif($cond = false, $if_true, $if_false){ if (!(is_bool($cond) && $if_true && $if_false)) return false; if ($cond) return $if_true; else return $if_false; } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/ Share on other sites More sharing options...
newbtophp Posted October 9, 2009 Author Share Posted October 9, 2009 Breakdown: ?><div style="clear: both"></div> </div> <div id="footer"> <div class="col1"></div> <?php wp_footer(); ?> </body></html><? </html><? << is uneeded because its not ended anywhere uneeded because not started anywhere >> ?><div style="clear: both"></div> maybe something along the lines of (wont work though): $file = preg_replace("?/>~([^']*?)~<\?", "~([^']*?)~", $file); Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-933928 Share on other sites More sharing options...
itaym02 Posted October 9, 2009 Share Posted October 9, 2009 Have no sleeve answer for you, just notice it is a common practice to leave unclosed PHP tags. This is done to avoid problems with white spaces. Now your problem becomes more complicated :-) Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-933933 Share on other sites More sharing options...
newbtophp Posted October 9, 2009 Author Share Posted October 9, 2009 Have no sleeve answer for you, just notice it is a common practice to leave unclosed PHP tags. This is done to avoid problems with white spaces. Now your problem becomes more complicated :-) If my whole file contains: ?><div style="clear: both"></div> </div> <div id="footer"> <div class="col1"></div> <?php wp_footer(); ?> </body></html><? Why would someone even add short tags when they are not used or needed, i understand the downside to this but in this situation i'd prefer to this. Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-933935 Share on other sites More sharing options...
newbtophp Posted October 10, 2009 Author Share Posted October 10, 2009 Anyone can help? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934079 Share on other sites More sharing options...
cags Posted October 10, 2009 Share Posted October 10, 2009 Looking at your first example of.. <?php if ( !function_exists(dynamic_sidebar) || !dynamic_sidebar(Bottom-Left) ) : ?> <?php endif; ?> I guess you could do a preg_replace matching something along the lines of... ~\?>\s*?<\?php~s But thats just one type. As I've said to your earlier post I don't especially see why you'd want to. It would probably be quicker to scan through the documents manually with a text editor that has search/replace, than to create accurate functionality (unless your doing an awfull lot of scripts). Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934265 Share on other sites More sharing options...
newbtophp Posted October 11, 2009 Author Share Posted October 11, 2009 Cheers cags but that did not work :-\ So I ended up doing 2 str_replaces: $file = str_replace('?><', '<', $file); $file = str_replace('><?', '>', $file); Which is insecure, because if their actually is two php tags joint together with php code it will remove them. Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934633 Share on other sites More sharing options...
itaym02 Posted October 11, 2009 Share Posted October 11, 2009 Can you give us the larger picture, as there might be a different approach to your problem. Think of an algorithm that scans one char at a time, and marks when a PHP tag is opened, it's place in the file and it type, and the same for closing tags. This way you would know which PHP tags are opened and do not have content in them, and which PHP tags are closed without first being opened. Some edge cases I can think off of the tip of my mind. <?php //?> echo 'watch out'; ?> <? echo 'watch out, if this is the last line in the file, then this is legit.'; <?='ggg'?> Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934663 Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 Cheers cags but that did not work :-\ So I ended up doing 2 str_replaces: $file = str_replace('?><', '<', $file); $file = str_replace('><?', '>', $file); Which is insecure, because if their actually is two php tags joint together with php code it will remove them. $out = preg_replace('~\?>\s*?<\?php~s', "", $input); Works fine on the example you gave... it simply looks for a closing tag followed by an open tag, with nothing but whitespace between them. Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934816 Share on other sites More sharing options...
newbtophp Posted October 11, 2009 Author Share Posted October 11, 2009 Cheers cags but that did not work :-\ So I ended up doing 2 str_replaces: $file = str_replace('?><', '<', $file); $file = str_replace('><?', '>', $file); Which is insecure, because if their actually is two php tags joint together with php code it will remove them. $out = preg_replace('~\?>\s*?<\?php~s', "", $input); Works fine on the example you gave... it simply looks for a closing tag followed by an open tag, with nothing but whitespace between them. Say if this is my file: ?><br class="clear" /> </div> <div id="footer-wrap"> <div id="footer"> <div class="span-3 append-1 small"> <?php if ( !function_exists(dynamic_sidebar) || !dynamic_sidebar(Bottom-Left) ) : ?> <?php endif; ?> </div><? The preg_replace makes no effect, the tags still display beside the divs (when their is no need) Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934842 Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 I never said it would match those. It was designed to remove a closing tag followed by any opening tag, which means neither is required. To remove them from the start and end of a file you could probably use something like... ~^[\s]*?\?>~ // and ~<\?(php)?[\s]*$~ Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934845 Share on other sites More sharing options...
newbtophp Posted October 11, 2009 Author Share Posted October 11, 2009 I never said it would match those. It was designed to remove a closing tag followed by any opening tag, which means neither is required. To remove them from the start and end of a file you could probably use something like... ~^[\s]*?\?>~ // and ~<\?(php)?[\s]*$~ Thanks added both works better, this is the output: ?><br class="clear" /> </div> <div id="footer-wrap"> <div id="footer"> <div class="span-3 append-1 small"> <?php if ( !function_exists(dynamic_sidebar) || !dynamic_sidebar(Bottom-Left) ) : ?> <?php endif; ?> </div> The tag still remains: ?><br class="clear" /> Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934847 Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 Works fine for me, tidied it up abit but don't believe I actually changed any functionality. function strip_excess_tags($input) { $pattern = array("~^\s*?\?>~", '~\?>(\s*)?<\?php~s', "~<\?(php)?\s*$~"); $replace = array("", "$1", ""); return preg_replace($pattern, $replace, $input); } Input: ?><br class="clear" /> </div> <div id="footer-wrap"> <div id="footer"> <div class="span-3 append-1 small"> <?php if ( !function_exists(dynamic_sidebar) || !dynamic_sidebar(Bottom-Left) ) : ?> <?php endif; ?> </div><? Output: <br class="clear" /> </div> <div id="footer-wrap"> <div id="footer"> <div class="span-3 append-1 small"> <?php if ( !function_exists(dynamic_sidebar) || !dynamic_sidebar(Bottom-Left) ) : endif; ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934859 Share on other sites More sharing options...
newbtophp Posted October 11, 2009 Author Share Posted October 11, 2009 Cags thats an awesome function, thanks alot. What would be the match for the following?, as you can see it ends with a php start tag, and I'd like to replace it with the actual end tag '?>' <?php function managerestaurants() { //check admin login $this->admin_model->checkAdminAccess(); $this->data['restaurantsData'] = $this->admin_model->getRestaurants(); $this->load->view('admin_manage_restaurants', $this->data); } }<? Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934864 Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 I really think you should explain why in gods name your trying to do these things, because there is no strict pattern to follow. To replace that specific value you could use... preg_replace('~<\?$~', '?>', $input); But that same pattern would screw up the previous match we made of removing <? from the end of the file and cannot be used concurrently with it. Basically what I'm saying is there is no point creating simple regex to match these very specific patterns if in the grand scheme of things they are all to be used on the same files. In order to successfully validate the code you would have to check if the open tag had a matching closing tag, if it didn't check for a closing tag that should be an open tag. This IMO will be awefully difficult to do accurately. The amount of effort in my opinion completely outweighs any advantage you might gain. Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934869 Share on other sites More sharing options...
newbtophp Posted October 11, 2009 Author Share Posted October 11, 2009 I really think you should explain why in gods name your trying to do these things, because there is no strict pattern to follow. To replace that specific value you could use... preg_replace('~<\?$~', '?>', $input); But that same pattern would screw up the previous match we made of removing <? from the end of the file and cannot be used concurrently with it. Basically what I'm saying is there is no point creating simple regex to match these very specific patterns if in the grand scheme of things they are all to be used on the same files. In order to successfully validate the code you would have to check if the open tag had a matching closing tag, if it didn't check for a closing tag that should be an open tag. This IMO will be awefully difficult to do accurately. The amount of effort in my opinion completely outweighs any advantage you might gain. Ok sure I decided to stick to your function and not use that regex as it would effect the previous regex from the function. Thanks for all the help Mind taking alook at?: http://www.phpfreaks.com/forums/index.php/topic,272414.0.html Cheers Quote Link to comment https://forums.phpfreaks.com/topic/177104-solved-remove-uneeded-php-tags/#findComment-934872 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.