Jump to content

preg_replace() pattern "/ /" delimiter will not work with $_POST variable.


sabinmash

Recommended Posts

preg_replace() asks that "Delimiter must not be alphanumeric or backslash" in the pattern.  So I changed

 

$new_text = preg_replace($_POST['withthis'] ,$_POST['withthis'],$_POST['text']);

to this

$replacethis = $_POST['replacethis'];
$new_text = preg_replace("/$replacethis/",$_POST['withthis'],$_POST['text']);

It works fine, but out of curiosity, is there any way to have the POST variable as a parameter directly, and why does it not work? 

 

Just to try it, I attempted: "/$_POST['withthis']/"  and $_POST["/'withthis'/"] and both do not work.  str_replace is a better option I think, but I am just trying to get a better understanding of this delimiter rule.  Thanks for your time!

Link to comment
Share on other sites

You'd have to escape any existing delimiters within the expression.

 

For example '~delim~iter~' is bad. '~delim\~iter~' is good.

 

You'd have to use str_replace to replace every instance of '~' with '\~' before you used it in the preg_* functions.

 

~ can be any valid delimiter.

Link to comment
Share on other sites

You'd have to escape any existing delimiters within the expression.

 

For example '~delim~iter~' is bad. '~delim\~iter~' is good.

 

You'd have to use str_replace to replace every instance of '~' with '\~' before you used it in the preg_* functions.

 

~ can be any valid delimiter.

 

True, but then again if the pattern already had escaped delimiters then you'd totally botch it up.

 

For example if the pattern was "~a string with \~ in it~" then it would end up "~a string with \\~ in it~".

 

So you'd need regex to validate your regex. o.O

Link to comment
Share on other sites

I think part of his question is also why he couldn't use the POST value directly inside the parens with the forwardslashes such as

"/$_POST['withthis']/"

 

As I am sure you know, you can use variables directly inside a double quoted string. But, when you have an array value with quotes around the key, PHP gets confused. The simple solution is to enclose the variables inside curly braces. I try to do this for ALL my variables within double quoted strings out of habit so I don't have to worry about the scenarios where they would fail.

 

This should work for you.

$new_text = preg_replace("/{$_POST['replacethis']}/" ,$_POST['withthis'],$_POST['text']);

Link to comment
Share on other sites

The simple solution is to enclose the variables inside curly braces. I try to do this for ALL my variables within double quoted strings out of habit so I don't have to worry about the scenarios where they would fail.

 

I find that syntax ugly, so I always concatenate.

 

Actually, I find double quotes to be ugly in general so I avoid them like the plague unless I need to use special characters (like \n).

Link to comment
Share on other sites

I find that syntax ugly, so I always concatenate.

 

Actually, I find double quotes to be ugly in general so I avoid them like the plague unless I need to use special characters (like \n).

 

Well, I find breaking in and out of quotes to concatenate things ugly. So, there. Thankfully, PHP supports both ways of working with strings/variables, your way and the right way  ;D

 

Plus, I find it hilarious when I see people do this (note the end of the string) which I see all the time.

$query = 'SELECT * FROM table_name WHERE field_id = ' . $fieldID . '';

 

But, to each his own.

 

EDIT: I suppose that breaking out of quotes to concatenate a variable would make sense if you cannot easily differentiate your variables inside the strings. But, that is an editor dependent issue. The editor I use makes it easy for me to 'see' the variables in the quoted strings. Something to think about if your editor does not do that for you.

Link to comment
Share on other sites

Thankfully, PHP supports both ways of working with strings/variables, your way and the right way  ;D

 

Pssshhh. :P

 

Plus, I find it hilarious when I see people do this (note the end of the string) which I see all the time.

$query = 'SELECT * FROM table_name WHERE field_id = ' . $fieldID . '';

 

I do use double quotes for queries like that, but I still concatenate it. So it would look like this:

$query = "SELECT * FROM table_name WHERE field_id = '" . $fieldID . "'";

 

Meh. I usually use a database wrapper so I hardly ever write a query like that anyway.

Link to comment
Share on other sites

Why wouldn't you just use

 

SELECT `whatever` FROM `table` WHERE `column` = "value"

 

I'm not sure about mysql_real_escape_string(), but I know for sure mysqli->escape_string() escapes doulbe quotes as well.

 

From what I understand, MySQL treats single and double quotes the same.

Link to comment
Share on other sites

$query = 'SELECT `whatever` FROM `table` WHERE `column` = "' . $value . '"';

 

I understand that. I was referring to why you'd use double quotes around a MySQL query string. Why wouldn't you continue to use single quotes, and instead double-quote strings within the query.

Link to comment
Share on other sites

I think you both missed what I was talking about. I see a lot of people that continually enter/exit the quoted string to concatenate variables where the string they are wanting to create needs to end with a variable. But, I routinely see them concatenating a null string in quotes at the end. Whenever I ask someone why they do that they never have a good answer. Usually something like "I thought it had to be that way".

 

Example:

$query = "SELECT * from tbl ORDER BY " . $sort_field . "";

 

lame

Link to comment
Share on other sites

I understand what you're saying.

 

I just notice a lot of people who use single quotes for strings switch to double quotes so their SQL queries can have single-quote string declarations. I was saying, why not simply use double-quotes for your SQL-strings.

Link to comment
Share on other sites

I think part of his question is also why he couldn't use the POST value directly inside the parens with the forwardslashes such as

"/$_POST['withthis']/"

 

As I am sure you know, you can use variables directly inside a double quoted string. But, when you have an array value with quotes around the key, PHP gets confused. The simple solution is to enclose the variables inside curly braces. I try to do this for ALL my variables within double quoted strings out of habit so I don't have to worry about the scenarios where they would fail.

 

This should work for you.

$new_text = preg_replace("/{$_POST['replacethis']}/" ,$_POST['withthis'],$_POST['text']);

 

Worked like a charm, thanks!

Link to comment
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.