1

I would like to know if there is a way to bind PHP function inside a regexp.

Example:

$path_str = '/basket.php?nocache={rand(0,10000)}';
$pattern = ? // something i have no idea
$replacement = ? // something i have no idea

$path = preg_replace($pattern, $replacement, $path_str);

Then :

echo "'$path'";

would produce something like

'/basket.php?nocache=123'

A expression not limited to the 'rand' function would be even more appreciated.

Thanks

Keil
  • 222
  • 2
  • 8
  • Why are you even considering using a regex? Just echo "/basket.php?nocache=" . rand(0,10000); – thnee Aug 24 '11 at 21:14
  • because "path_str" will be stored in DB, but at some later execution of a cron job, i want to process path_str – Keil Aug 24 '11 at 21:17
  • ok, i found what i needed : preg_replace_callback function – Keil Aug 24 '11 at 21:26
  • 1
    Storing function names in database? Horrible, absolutely terrible, disgusting. You should halt all operations immediately and go back to the drawing board. – thnee Aug 24 '11 at 21:29
  • you're wrong, there is nothing horrible to store any logic clutch in db or somewhere else. How would you be able to share some code between many languages. For me it is simple : protocol + (language + interpreter) – Keil Aug 27 '11 at 12:30

3 Answers3

1

You could do the following. Strip out the stuff in between the {} and then run an eval on it and set it to a variable. Then use the new variable. Ex:

$str = "/basket.php?nocache={rand(0,10000)}";
$thing = "rand(0,10000)";
eval("\$test = $thing;");
echo $test;

$thing would be what's in the {} which a simple substr can give you. $test the becomes the value of executing $thing. When you echo test, you get a random number.

Joey Rivera
  • 16
  • 1
  • 4
  • @joey: I'm not fond of eval, but this solution is probably the simplier that i've saw here. – Keil Aug 27 '11 at 12:35
  • @graydot: explain yourself. What are the cases when eval would become dangerous. Please don't just repeat what you have heard about eval, but tell us your very personal love story you had with this function. – Keil Aug 27 '11 at 12:38
  • @Keil Have a look at [this question](http://stackoverflow.com/q/3499672/706138) and it's [accepted answer](http://stackoverflow.com/questions/3499672/when-if-ever-is-eval-not-evil/4096970#4096970). It doesn't simply state that `eval` is absolutely horrible, but explains that in most cases, `eval` is absolutely not the answer. – adlawson Aug 28 '11 at 00:04
1

Don't, whatever you do, store PHP logic in a string. You'll end up having to use eval(), and if your server doesn't shoot you for it, your colleagues will.

Anywhoo, down to business.

Your case is rather simple, where you need to append a value to the end of a string. Something like this would be sufficient

$stored = '/basket.php?nocache=';
$path   = $stored . rand(0,10000);

If, however, you need to place a value somewhere in the middle of a string, or possibly in a variable location, you could have a look at sprintf()

$stored = '/basket.php?nocache=%d&foo=bar';
$path   = sprintf($stored, rand(0,10000));
adlawson
  • 6,303
  • 1
  • 35
  • 46
  • My case is not simple. You just think it is, that is why you give me an alternative. It is not what I've asked. But thanks anyway – Keil Aug 27 '11 at 12:33
0

I would not try to store functions in a database. Rather store some kind of field that represents the type of function to use for each particular case.

Then inside your crontab you can do something like:

switch ($function)
{
    case 'rand':
    $path_str = '/basket.php?nocache='. rand(0,10000);
}

e.t.c

Paul S.
  • 1,527
  • 9
  • 13