php_fish Posted May 7, 2009 Share Posted May 7, 2009 I'm trying to get preg_replace to find a fucntion within a string and then evaluate it as php code but I can't get it to work; I can't find an answer to this on the net, I've tried for days. Here's some sample code; function tester($var){ //return($var); return $var; } $str = "tester('test string')"; $re = '/\(tester{1,1}[.]*\)/em'; $fnc_output = preg_replace($re,'$1',$str); echo($fnc_output); It's not evaluating the function. From my understanding and research it should work so I think I'm making a basic fundemental mistake somewhere (?) Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/ Share on other sites More sharing options...
Potatis Posted May 7, 2009 Share Posted May 7, 2009 Yes a "fucntion" is a function that doesn't work. I can't see your code is doing anything at all. What is $1? Is there an example of a function you are trying to find and replace? Do you even want to replace it? ??? Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828306 Share on other sites More sharing options...
php_fish Posted May 7, 2009 Author Share Posted May 7, 2009 Yes a "fucntion" is a function that doesn't work. I can't see your code is doing anything at all. What is $1? Is there an example of a function you are trying to find and replace? Do you even want to replace it? ??? A function is a function? What does that mean? In theory the code should work but I'm overlooking something and I'd need to find out what that is. I've simplified the example given and would like to make that simplified example work. Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828308 Share on other sites More sharing options...
Potatis Posted May 7, 2009 Share Posted May 7, 2009 A function is a function? What does that mean? I was having fun with your spelling mistake, and the pronunciation of the typo which could reflect how one might feel about a function that doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828311 Share on other sites More sharing options...
Adam Posted May 7, 2009 Share Posted May 7, 2009 Try this.. $str = "tester('test string')"; preg_match_all('/[a-zA-Z_][\w]+\([^\)]+\);/', $str, $matches); print_r($matches); You should be able to convert it to preg_replace if you need to, I just didn't understand what you were trying to do before... Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828313 Share on other sites More sharing options...
php_fish Posted May 7, 2009 Author Share Posted May 7, 2009 Try this.. $str = "tester('test string')"; preg_match_all('/[a-zA-Z_][\w]+\([^\)]+\);/', $str, $matches); print_r($matches); You should be able to convert it to preg_replace if you need to, I just didn't understand what you were trying to do before... It doesn't do what I want it to. I want it to pick out a function in a string then to execute the function and assign the result to a variable. Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828341 Share on other sites More sharing options...
Adam Posted May 7, 2009 Share Posted May 7, 2009 Well you now have an array containing all of the functions captured. What have you tried so far? Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828351 Share on other sites More sharing options...
php_fish Posted May 7, 2009 Author Share Posted May 7, 2009 Well you now have an array containing all of the functions captured. What have you tried so far? I need it to execute the functions in the string, eg a paragraph of text, replace functions with their output. I don't want to collect the functions then execute them, they have to be executed in situ so to speak Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828372 Share on other sites More sharing options...
sasa Posted May 7, 2009 Share Posted May 7, 2009 are you trying this <?php function tester($var){ return $var; } $function = "tester"; $re = $function('sasa'); echo $re; ?> Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828434 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 $re = '/\(tester{1,1}[.]*\)/em'; Just out of curiosity, is {1,1} needed? Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828457 Share on other sites More sharing options...
php_fish Posted May 7, 2009 Author Share Posted May 7, 2009 are you trying this <?php function tester($var){ return $var; } $function = "tester"; $re = $function('sasa'); echo $re; ?> No Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828500 Share on other sites More sharing options...
php_fish Posted May 7, 2009 Author Share Posted May 7, 2009 $re = '/\(tester{1,1}[.]*\)/em'; Just out of curiosity, is {1,1} needed? No it's not needed, but it's not the regular expression I am trying to improve. Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828504 Share on other sites More sharing options...
Adam Posted May 7, 2009 Share Posted May 7, 2009 Could you give a realistic example of what the input would look like? I was thinking something along the lines of this may work... $str = "strtoupper('test string');"; $str = preg_replace('/([a-zA-Z_][\w]+\([^\)]+\);)/', "<?php $1 ?>", $str); eval($str); But that would require PHP functions without PHP tags around them, just in a string. (Not tested!) Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828526 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Take out the <?php ?> in eval statement unless you want a parse error. Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828535 Share on other sites More sharing options...
php_fish Posted May 7, 2009 Author Share Posted May 7, 2009 Could you give a realistic example of what the input would look like? I was thinking something along the lines of this may work... $str = "strtoupper('test string');"; $str = preg_replace('/([a-zA-Z_][\w]+\([^\)]+\);)/', "<?php $1 ?>", $str); eval($str); But that would require PHP functions without PHP tags around them, just in a string. (Not tested!) An example of what the input may be; $str = "This is a paragraph of text with tester('a string var') functions thrown in amongst it. In the anotherFnc('more str values') hope that this paragraph is returned and the functions interspersed amongst it are evaluted and replaced with their return values" Adam, you will also need the 'e' modifier at the end of your regex Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828569 Share on other sites More sharing options...
thebadbad Posted May 7, 2009 Share Posted May 7, 2009 I perfectly understand what you're trying to do php_fish, and I got it kinda working: <?php function tester($var) { return $var; } $str = "tester('test string')"; $fnc_output = preg_replace('~(tester)\(\'(.*?)\'\)~e', "$1('$2')", $str); echo $fnc_output; ?> Outputs test string. The problem is however, that I had to include the quotes in the pattern and replacement, else I got a whole bunch of errors about backslashes and quotes being illegal in some 'input'. Code (that I think should work, but doesn't): <?php function tester($var) { return $var; } $str = "tester('test string')"; $fnc_output = preg_replace('~(tester\(.*?\))~e', '$1', $str); echo $fnc_output; ?> Errors: Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\XAMPP\htdocs\test\regex8.php(6) : regexp code on line 1 Warning: Unexpected character in input: ''' (ASCII=39) state=1 in C:\XAMPP\htdocs\test\regex8.php(6) : regexp code on line 1 Parse error: syntax error, unexpected T_STRING in C:\XAMPP\htdocs\test\regex8.php(6) : regexp code on line 1 Fatal error: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Failed evaluating code: tester(\'test string\') in C:\XAMPP\htdocs\test\regex8.php on line 6 Can anyone elaborate on the first two errors? Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828571 Share on other sites More sharing options...
thebadbad Posted May 7, 2009 Share Posted May 7, 2009 And php_fish, your pattern doesn't work because you're escaping the parenthesis that are supposed to capture the match and put it in $1. Edit: To be honest, your whole pattern doesn't make sense. Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828575 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Do you have to use preg_replace? preg_match_all('#((?<func>\w+)?\s?\([\'\"]?.*?[\'\"]?\))#', $str, $matches) Something along that? You can then use function_exists on $matches['func'] Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828576 Share on other sites More sharing options...
thebadbad Posted May 7, 2009 Share Posted May 7, 2009 Found an article explaining why my second function didn't work: http://www.procata.com/blog/archives/2005/11/13/two-preg_replace-escaping-gotchas/ Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-828586 Share on other sites More sharing options...
php_fish Posted May 11, 2009 Author Share Posted May 11, 2009 Found an article explaining why my second function didn't work: http://www.procata.com/blog/archives/2005/11/13/two-preg_replace-escaping-gotchas/ Thanks for your work on this. You have got it working but I need it to work without the brackets, so $1('$2') becomes $1$2. But it doesn't work that way for some reason. Just can't work out why. Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-831907 Share on other sites More sharing options...
thebadbad Posted May 11, 2009 Share Posted May 11, 2009 You're probably best off using preg_replace_callback() or preg_match_all(), i.e. capture the function calls and then run them within a separate function, and return the replacement: <?php $str = "a b c tester('test string') d e f"; function tester($var) { return $var; } function eval_function($matches) { return eval("return {$matches[1]}({$matches[2]});"); } echo preg_replace_callback('~([_a-z][_a-z0-9]*)\s?\(([\'"]?.*?[\'"]?)\)~is', 'eval_function', $str); ?> Output: a b c test string d e f Quote Link to comment https://forums.phpfreaks.com/topic/157196-evaluate-a-funcyion-with-e-modifier-and-preg-replace/#findComment-831987 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.