mrdhood Posted January 9, 2012 Share Posted January 9, 2012 I know this should be simple but I am terrible with preg_replace and keep returning blank results. I'm attempting to make "{table}" change to the value of $tables['table'], for example: "SELECT * FROM `{settings}`" would change to "SELECT * FROM `" . $tables['settings'] ."` Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/ Share on other sites More sharing options...
ragax Posted January 10, 2012 Share Posted January 10, 2012 I tested this code, it works. $subject='"SELECT * FROM `{settings}`"'; $s=preg_replace('/"SELECT \* FROM `\{([^}]+)\}`"/', '"SELECT * FROM `".$tables[\'\1\']"', $subject); echo $s; Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305968 Share on other sites More sharing options...
mrdhood Posted January 10, 2012 Author Share Posted January 10, 2012 I was trying to use $1 :/. I had almost the same thing at one point. How would I go about just changing {table} to $tables[\1\]? That way the following examples wouldn't be bothered: SELECT `id` FROM `{table}` SELECT * FROM `{table}` WHERE `id`='1' and so on. Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305975 Share on other sites More sharing options...
mrdhood Posted January 10, 2012 Author Share Posted January 10, 2012 I imagined it'd be something like: $query = preg_replace('`\{([^}]+)\}`', "self::$config['tables'][$1]", $query); but that errors as Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in and $query = preg_replace('`\{([^}]+)\}`', '"self::$config[\'tables\'][$1]'", $query); as Parse error: syntax error, unexpected '"' in Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305979 Share on other sites More sharing options...
ragax Posted January 10, 2012 Share Posted January 10, 2012 How would I go about just changing {table} to $tables[\1\] If that's literally what you want, no problem, but are you sure you literally want $tables[\1] ??? What does the \1 refer to? (How is it going to work in your SELECT statement?) Anyhow: $subject='"SELECT * FROM `{table}`"'; $s=preg_replace('/"SELECT \* FROM `\{table}`"/', '"SELECT * FROM `".$tables[\\\\1]"', $subject); Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305980 Share on other sites More sharing options...
mrdhood Posted January 10, 2012 Author Share Posted January 10, 2012 $tables = array( 'settings' => $prefix . 'settings', 'users' => $prefix . 'users' ); $query = "SELECT * FROM `{users}` WHERE `user_id`='" . $Visitor->user_id . "'"; // Should be $query = "SELECT * FROM `" . $tables['users'] . "` WHERE `user_id`='" . $Visitor->user_id . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305986 Share on other sites More sharing options...
ragax Posted January 10, 2012 Share Posted January 10, 2012 How is this different from the first solution I gave you? As requested, among other things, it replaces whatever is in the braces: {settings}, {users} or whatever with $tables['settings'], $tables['users'] or whatever. Please be as clear as possible as it is very hard to troubleshoot regex without extremely clear communication about desired input and output. Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305990 Share on other sites More sharing options...
mrdhood Posted January 10, 2012 Author Share Posted January 10, 2012 How is this different from the first solution I gave you? As requested, among other things, it replaces whatever is in the braces: {settings}, {users} or whatever with $tables['settings'], $tables['users'] or whatever. Please be as clear as possible as it is very hard to troubleshoot regex without extremely clear communication about desired input and output. In all honesty I don't know.. static function Execute($query) { $tables = self::$config['tables']; $subject='"SELECT * FROM `{settings}`"'; //echo self::$config['tables']['settings']; echo $tables['settings']; $query = preg_replace('/"SELECT \* FROM `\{([^}]+)\}`"/', '"SELECT * FROM `".$tables[\'\1\']"', $subject); echo $query . "<br />"; } is what I have right now. then I call Database::Execute("SELECT * FROM `{settings}`"); and it outputs this: SELECT * FROM `{settings}` Table 'sports_sports.{settings}' doesn't exist Thank you for your help and patience thus far by the way. Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305992 Share on other sites More sharing options...
ragax Posted January 10, 2012 Share Posted January 10, 2012 1. In your code, you have an echo $query; Does it output the query string you expect? In other words, the first question is: Does the regex work according to the specs you specified? You may have other problems (talking to the database), but first, we need to know: does the regex do what you wanted it to do? 2. We prepared a query string ($query) according to your specs using regex, and I have no idea if the syntax of the query itself is correct, I only looked at your specs. But you seem to be executing a different query altogether anyhow. I was expecting to see something like: $result=$db->query($query); Not Execute(a query completely unrelated to $query) (The query method above assumes you have already created a database object, at its simplest form: $db= new PDO ($dsn,$dbUser,$dbPass); You are probably using another database interface.) Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305995 Share on other sites More sharing options...
mrdhood Posted January 10, 2012 Author Share Posted January 10, 2012 1. In your code, you have an echo $query; Does it output the query string you expect? In other words, the first question is: Does the regex work according to the specs you specified? You may have other problems (talking to the database), but first, we need to know: does the regex do what you wanted it to do? No it's outputting what I inputted: "SELECT * FROM `{settings}`" 2. We prepared a query string ($query) according to your specs using regex, and I have no idea if the syntax of the query itself is correct, I only looked at your specs. But you seem to be executing a different query altogether anyhow. I was expecting to see something like: $result=$db->query($query); Not Execute(a query completely unrelated to $query) (The query method above assumes you have already created a database object, at its simplest form: $db= new PDO ($dsn,$dbUser,$dbPass); You are probably using another database interface.) Execute($query): $query = "SELECT * FROM `{settings}`" Which is exactly what I told you it would be, I'm just trying to change {settings} to $tables['settings'] Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305996 Share on other sites More sharing options...
ragax Posted January 10, 2012 Share Posted January 10, 2012 Can you please try an exact copy-paste on my code from the first post, as I don't want to go blind trying to see if there is a difference with yours: $subject='"SELECT * FROM `{settings}`"'; $s=preg_replace('/"SELECT \* FROM `\{([^}]+)\}`"/', '"SELECT * FROM `".$tables[\'\1\']"', $subject); echo $s; Here is the output I get: "SELECT * FROM `".$tables['settings']" Please let me know if you do not get that. Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305997 Share on other sites More sharing options...
ragax Posted January 10, 2012 Share Posted January 10, 2012 Also, I see in the output string the the string continuation dot and the `"grave accent" are missing, so you can change the pattern to this: $s=preg_replace('/"SELECT \* FROM `\{([^}]+)\}`"/', '"SELECT * FROM `".$tables[\'\1\']."`', $subject); Keep the echo and tell me if you get this output: "SELECT * FROM `".$tables['settings']."` Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305998 Share on other sites More sharing options...
mrdhood Posted January 10, 2012 Author Share Posted January 10, 2012 Ok I see the issue: yes it's outputting "$tables['settings']" but I need it to output the actual value. Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305999 Share on other sites More sharing options...
mrdhood Posted January 10, 2012 Author Share Posted January 10, 2012 SELECT * FROM `{settings}` is the result of that echo with the regex from the last post. Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1306000 Share on other sites More sharing options...
ragax Posted January 10, 2012 Share Posted January 10, 2012 yes it's outputting "$tables['settings']" Great. So the expression is working. SELECT * FROM `{settings}` is the result of that echo My echo is "SELECT * FROM `".$tables['settings']."` Maybe a copy-paste problem? The main thing is that the first one works. Starting from there, you can introduce the dot (.) and the grave (`) into the expression that works, in the places where I had them, in order to get around the problem. Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1306002 Share on other sites More sharing options...
PFMaBiSmAd Posted January 10, 2012 Share Posted January 10, 2012 To actually replace your {name} placeholder in the string with the contents of a php variable, you must use the /e modifier. Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1306003 Share on other sites More sharing options...
mrdhood Posted January 10, 2012 Author Share Posted January 10, 2012 $query = preg_replace('/{(.*?)}/e', '$tables[\'$1\']', $query); worked. thank you Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1306005 Share on other sites More sharing options...
iwpg Posted November 25, 2013 Share Posted November 25, 2013 (edited) $query = preg_replace('/{(.*?)}/e', '$tables[\'$1\']', $query);worked. thank you And here we go :-) Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead This is killing me. I was confused, finally got the hang of the good ol preg_replace function, and now the "e" modifer requires a callback. I am using an older CMS system that is no longer supported, so what this line of code actually accomplishes is beyond me. This is what is making this more difficult for me. My guess is that it allows PHP code to execute inside a template. This is what I have originally, any help to get this translated to preg_replace_callback will be so much appreciated! $template = preg_replace("/\?".">(\r*\n*.*)(<\?php|<\?)/esiU","addtemplateslashes('\\1')",$template); Edited November 25, 2013 by iwpg Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1459885 Share on other sites More sharing options...
iwpg Posted November 25, 2013 Share Posted November 25, 2013 This is what I have so far, but I don't know what to do with addtemplateslashes if (!function_exists('parseTagsRecursive')) { function parseTagsRecursive($template) { $regex = '#\/\?".">(\r*\n*.*)(<\?php|<\?)/esiU#'; if (is_array($template)) { $template = $template[1]; } return preg_replace_callback($regex, 'parseTagsRecursive', $template); } } $template = parseTagsRecursive($template); Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1459978 Share on other sites More sharing options...
iwpg Posted November 25, 2013 Share Posted November 25, 2013 Please delete my last 2 posts. The thread was marked as answered. New post is located at http://forums.phpfreaks.com/topic/284259-preg-replace-callback-help/ Quote Link to comment https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1459985 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.