iwpg Posted November 25, 2013 Share Posted November 25, 2013 This is what I have originally, any help to get this translated to preg_replace_callback will be so much appreciated! function addtemplateslashes($template) { return "?".">".$template."<?php"; } $template = preg_replace("/\?".">(\r*\n*.*)(<\?php|<\?)/esiU","addtemplateslashes('\\1')",$template); And this is what I have so far with no luck. if (!function_exists('parseTagsRecursive')) { function parseTagsRecursive($template) { $regex = '#\/\?".">(\r*\n*.*)(<\?php|<\?)#'; if (is_array($template)) { $template = $template[1]; } return preg_replace_callback($regex, 'parseTagsRecursive', $template); } } $template = parseTagsRecursive($template); Quote Link to comment https://forums.phpfreaks.com/topic/284259-preg-replace-callback-help/ Share on other sites More sharing options...
Ch0cu3r Posted November 25, 2013 Share Posted November 25, 2013 (edited) All you need was function addtemplateslashes($text) { return "?".">".$text[1]."<?php"; } $template = preg_replace_callback("/\?".">(\r*\n*.*)(<\?php|<\?)/siU",'addtemplateslashes', $template); Edited November 25, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284259-preg-replace-callback-help/#findComment-1459992 Share on other sites More sharing options...
iwpg Posted November 25, 2013 Author Share Posted November 25, 2013 Thank you for your help! Good to go! Quote Link to comment https://forums.phpfreaks.com/topic/284259-preg-replace-callback-help/#findComment-1460000 Share on other sites More sharing options...
requinix Posted November 25, 2013 Share Posted November 25, 2013 (edited) Heck, you don't even need callbacks for it at all. And don't worry about the ?>s: PHP's smart enough to know not to stop processing in the middle of a string. And the only difference between the search and replacement string is using the long open tags, so don't bother searching for the long ones. To avoid matching [ic]<?php[/i], use a negative assertion: "there must not be a 'php' at this point". $template = preg_replace("/\?>(\r*\n*.*)$1 Edited November 25, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/284259-preg-replace-callback-help/#findComment-1460004 Share on other sites More sharing options...
iwpg Posted November 25, 2013 Author Share Posted November 25, 2013 (edited) I love it, thanks! This just made my day!!! Edited November 25, 2013 by iwpg Quote Link to comment https://forums.phpfreaks.com/topic/284259-preg-replace-callback-help/#findComment-1460028 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.