note 96263 modified in book.simplexml by danbrown

From: Date: Wed, 17 Feb 2010 18:28:34 +0000
Subject: note 96263 modified in book.simplexml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166089@lists.php.net to get a copy of this message
dynamic sql in php using xml: test.xml: <?xml version="1.0" encoding="UTF-8"?> <sql> <statement> SELECT * FROM USERS <call criteria="byId">WHERE id = %d</call> <call criteria="byUsername">WHERE username = "%s"</call>; </statement> </sql> index.php: <?php function callMe($param) { $search = array('byUsername' => 'dynsql'); if (isset($search[$param[1]])) { return sprintf($param[2], $search[$param[1]]); } return ""; } $xml = simplexml_load_file("test.xml"); $string = $xml->statement->asXML(); $string = preg_replace_callback('/<call criteria="(\w+)">(.*?)<\/call>/', 'callMe', $string); $node = simplexml_load_string($string); echo $node; ?> obviously, this example can be improved [in your own code.] --was-- dynamic sql in php using xml: //test.xml <?xml version="1.0" encoding="UTF-8"?> <sql> <statement> SELECT * FROM USERS <call criteria="byId">WHERE id = %d</call> <call criteria="byUsername">WHERE username = "%s"</call>; </statement> </sql> //index.php function callMe($param) { $search = array('byUsername' => 'dynsql'); if (isset($search[$param[1]])) { return sprintf($param[2], $search[$param[1]]); } return ""; } $xml = simplexml_load_file("test.xml"); $string = $xml->statement->asXML(); $string = preg_replace_callback('/<call criteria="(\w+)">(.*?)<\/call>/', 'callMe', $string); $node = simplexml_load_string($string); echo $node; obviously, this example can be improved http://php.net/manual/en/book.simplexml.php

« previous php.notes (#166089) next »