Jump to content

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

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

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.

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

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.

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

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

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

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?

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.

 

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

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.