Jump to content

preg_replace_callback help


k8jsl

Recommended Posts

i  have the following but of coarse its now depreciated

icao = (!empty($this->form['icao'])) ? $this->form['icao'] : $this->conf['icao1x'];

                          $tzoff = preg_replace('/-/', '', $this->conf['tzoff']);

                          $day = (!empty($this->form['day'])) ? $this->form['day'] : '1';

                          $this->day = $day;
$run = (!empty($this->form['run'])) ? $this->form['run'] : $this->conf['run'];                          

                        $url = $this->runconf[$run . '_url'];

                        $post = $this->runconf[$run . '_url_tail'];



                        list($this->temp, $cachet, $runner) = explode("|", $this->runconf[$run . '_settings']);



                          $post = @preg_replace("/\{(\w+)\}/e", '$$1', $post);
 

                        $cachepath = @preg_replace("/\{(\w+)\}/e", '$$1', $this->runconf[$run . '_cache']);

$post IS defined as:

obhistory.php?icao={icao}&day={day}&offset={tzoff}
 

Link to comment
Share on other sites

Depending on the rest of your code it may not actually be possible to do this in a reasonable way.

 

Fortunately you should take a slightly different approach to it. Rather than allow anything as a variable, which is extremely dangerous, you should use a whitelist of values. At that point it's really just an array of names and values. Pass that to the callback function.

$values = array(
	"icao" => $icao,
	"tzoff" => $tzoff,
	"day" => $day,
	// etc
);

$post = preg_replace_callback('/\{(\w+)\}/', function($match) use ($values) {
	if (isset($values[$match[1]])) {
		return $values[$match[1]];
	} else {
		return $match[0]; // no change
		// return ""; // empty
		// or whatever you want to do
	}
}, $post);
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.