Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using preg_replace with variable parameters and functions 1

Status
Not open for further replies.

tkwoof

IS-IT--Management
Oct 26, 2000
3
GB
I am having a problem with returned values from a database.
In essence what I am doing is storing function information in a table
I then have a function that pulls the information to execute the function in the table such as

in the table I have

function name
parameter list

the data is returned in a string like this:
$name= "$function_name" . "|" . "$variable_string" . "|" . "$variable_count";
I then split the string into the component parts like this:
$check_q = preg_split("/[|]+/", $name);
$name = $check_q[0];
$vars = $check_q[1];
$varc = $check_q[2];

I can now echo $name(); and the stored function will execute.
The other items returned are $vars a list of parameters and $varc a count of the parameters. In the example above
$variable_string = "$PHP_AUTH_USER,$Last_Name,$First_Name";
The variable names in the string are not the values associated with the named variables but just the names of the variables. Therein lies the problem. I need to get the values of those variables (They are global on the page so the values are available) into the parameter list of
echo $name(); This also needs to be done for a varying number of parameters, and I can't hardcode the parameter list either.

It is possible to retrieve the values using preg_replace(); such as:
$data = preg_replace('/(\$AXS)/',$AXS, $data);
but I need to be able to do it like this
$data = preg_replace('/(Read name from this variable)/',(retrive the actual value of the named variable), $data);

I have two problems here, one is getting the ('/(\$AXS)/' section of the preg_replace to read the value of the variable instead of treating it like a pattern. I have tried ('/($AXS)/' and ($AXS and ("$AXS" and several other iterations and have come up empty.

The other is the ,$AXS, section I need to be able to tell it which variable I want the value for without having to write the variable name. I have thought of using an array here and use the count of parameters to build it prior to retreiving the values, but I cannot figure out how to make this work.

Anyway if you can help please let me know.

Thanks

Scott
 
For the execution part, check out eval: Sample usage:
eval("echo $name($args)");

Now, to replace variables. I *very* recently did this, so it should work:
[tt]$string = preg_replace("/\\\$([A-Za-z0-9_-]+)/e", "((isset(\$\\1)) ? \$\\1 : '\$\\1')", $string);[/tt]
The [tt]e[/tt] modifier for the pattern tells [tt]preg_replace[/tt] that the replace argument contains PHP code, which should be evaluated. The replace argument then checks for the existance of the variable, and if it exists, it puts the variables value in there. If not, put the [tt]$<whatever>[/tt] back in.

//Daniel
 
Daniel,
That did the trick!
It works great.
Thank you very much.
Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top