Jump to content

[SOLVED] String Replacements and Code Evaluation


friedemann_bach

Recommended Posts

Dear all,

 

I have a function here that executes a MYSQL query by submitting a query string. I want the query string to contain simplified references, like this:

 

$my_object->ExecuteQuery ("SELECT id FROM users WHERE name='{name}'");

 

where {name} should be replaced by a reference to the same object, like

 

$my_object->SomeData[name]

 

so that the query reads as follows, assumed that the array "SomeData" contains the name 'henry':

 

"SELECT id FROM users WHERE name='henry'"

 

Thinking that this would be easy I tried to use preg_replace:

 

function ExecuteQuery ($querystring)
  ...
  $querystring = preg_replace('/\{(\w*)\}/','$my_object->SomeData[$1]',$querystring);

 

There seems to be a problem with the reference: I am not able to use the string found by the regular expression as a part of the variable reference ($myobject->SomeData[$1]). Does anybody have a solution? I tried also with a callback function, but it produces nothing except the same problem.

 

Thanks a lot!

 

I wrote up a quick Query class out of boredom actually that basically accomplishes what you're trying to do:

 

<?php
class Query {
public $delimeter;
protected $query;
protected $subs;

public function __construct()
{
	$this->delimeter = ':';
	$this->query = NULL;
	$this->subs = array();
}

public function reset()
{
	$this->query = NULL;
	$this->subs = array();
}

public function setQuery($query)
{
	$this->query = $query;
}

public function add(array $items)
{
	foreach ($items as $find => $replace) {
		$this->subs[$find] = $replace;
	}
}

public function prepare()
{
	if (!$this->query) {
		throw new Exception('No query was set.');
		return null;
	}

	foreach ($this->subs as $find => $replace) {
		$to_replace = $this->delimeter . $find . $this->delimeter;
		$this->query = str_replace($to_replace, $replace, $this->query);
	}

	return $this->query;
}
}
?>

 

Usage:

<?php
$query = new Query();
$query->setQuery('SELECT * FROM users WHERE name=':name:'");
$query->add(array('name' => $name));
$database->query($query->prepare());
$query->reset();
//etc
?>

@effigy:

 

You need the /e modifier: /\{(\w*)\}/e.

 

Great. Many thanks for your help. That works. You made my day!

 

 

@lemmin:

 

$my_object->ExecuteQuery ("SELECT id FROM users WHERE name='{$my_object->SomeData[name]}'");

 

Yes, of course that would be possible. However, the method which is here called "ExecuteQuery" is part of a framework. I want it to be simplified so that other developers do not need to reproduce the class references for each field they want to refer to.

 

 

@DarkWater:

 

That's a very nice solution. It's a totally different approach but it work fine too. In this context I prefer the solution submitted by effigy. Thanks anyway!

 

Archived

This topic is now archived and is closed to further replies.

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