sabinmash Posted December 15, 2011 Share Posted December 15, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/ Share on other sites More sharing options...
scootstah Posted December 15, 2011 Share Posted December 15, 2011 Sure, if the $_POST variable contains the delimiters. $_POST['pattern'] = '/hello/'; $foo = 'hello world'; $foo = preg_replace($_POST['pattern'], '', $foo); echo $foo; Outputs " world". Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298277 Share on other sites More sharing options...
xyph Posted December 16, 2011 Share Posted December 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298340 Share on other sites More sharing options...
scootstah Posted December 16, 2011 Share Posted December 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298351 Share on other sites More sharing options...
Psycho Posted December 16, 2011 Share Posted December 16, 2011 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']); Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298356 Share on other sites More sharing options...
scootstah Posted December 16, 2011 Share Posted December 16, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298358 Share on other sites More sharing options...
Psycho Posted December 16, 2011 Share Posted December 16, 2011 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 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. Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298364 Share on other sites More sharing options...
scootstah Posted December 16, 2011 Share Posted December 16, 2011 Thankfully, PHP supports both ways of working with strings/variables, your way and the right way Pssshhh. 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. Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298365 Share on other sites More sharing options...
xyph Posted December 16, 2011 Share Posted December 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298388 Share on other sites More sharing options...
scootstah Posted December 16, 2011 Share Posted December 16, 2011 We're talking about storing the query in a variable. Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298394 Share on other sites More sharing options...
xyph Posted December 16, 2011 Share Posted December 16, 2011 $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. Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298444 Share on other sites More sharing options...
Psycho Posted December 16, 2011 Share Posted December 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298457 Share on other sites More sharing options...
xyph Posted December 16, 2011 Share Posted December 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298468 Share on other sites More sharing options...
scootstah Posted December 16, 2011 Share Posted December 16, 2011 I don't know. Just stuck with me from all those years ago I guess. Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298473 Share on other sites More sharing options...
sabinmash Posted December 16, 2011 Author Share Posted December 16, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/253264-preg_replace-pattern-delimiter-will-not-work-with-_post-variable/#findComment-1298606 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.