Jump to content

Evaluate a funcyion with e modifier and preg replace


php_fish

Recommended Posts

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 (?)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.