Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305975
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305979
Share on other sites

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);

 

 

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305980
Share on other sites

$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 . "'";

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305986
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305990
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305992
Share on other sites

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.)

 

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305995
Share on other sites

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']

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305996
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305997
Share on other sites

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']."`

 

 

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1305998
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1306002
Share on other sites

  • 1 year later...
$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 by iwpg
Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1459885
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/254686-simple-preg_replace/#findComment-1459978
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.