friedemann_bach Posted August 6, 2008 Share Posted August 6, 2008 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! Link to comment https://forums.phpfreaks.com/topic/118457-solved-string-replacements-and-code-evaluation/ Share on other sites More sharing options...
effigy Posted August 6, 2008 Share Posted August 6, 2008 You need the /e modifier: /\{(\w*)\}/e. Why not use prepared statements? Link to comment https://forums.phpfreaks.com/topic/118457-solved-string-replacements-and-code-evaluation/#findComment-609766 Share on other sites More sharing options...
lemmin Posted August 6, 2008 Share Posted August 6, 2008 Why wouldn't you just do this? $my_object->ExecuteQuery ("SELECT id FROM users WHERE name='{$my_object->SomeData[name]}'"); Link to comment https://forums.phpfreaks.com/topic/118457-solved-string-replacements-and-code-evaluation/#findComment-609768 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 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 ?> Link to comment https://forums.phpfreaks.com/topic/118457-solved-string-replacements-and-code-evaluation/#findComment-609773 Share on other sites More sharing options...
friedemann_bach Posted August 7, 2008 Author Share Posted August 7, 2008 @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! Link to comment https://forums.phpfreaks.com/topic/118457-solved-string-replacements-and-code-evaluation/#findComment-610710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.