DssTrainer Posted March 30, 2009 Share Posted March 30, 2009 I have a sql string: "select * from table where language_id = '5' and date = now()"; I want to change language_id = '5' to language_id = '1' I've been playing with it all day, but I can't even find a regex reference example that does what I'm trying to do. Any help would be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/ Share on other sites More sharing options...
.josh Posted March 30, 2009 Share Posted March 30, 2009 umm...if you want it to always be 1, just hardcode it as 1 instead of 5.... if the id is going to change, that's not regex, that's using a variable, like $id = 1; ... "select * from table where language_id = '$id' and date = now()"; Quote Link to comment https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/#findComment-797145 Share on other sites More sharing options...
DssTrainer Posted March 30, 2009 Author Share Posted March 30, 2009 Thanks. Not to be rude, but I know what I want. I've summarized my real code to the meat and potatoes of the regex. I'm just looking for the answer to my question. Quote Link to comment https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/#findComment-797180 Share on other sites More sharing options...
.josh Posted March 30, 2009 Share Posted March 30, 2009 well maybe you should un-summarize it, because it sure sounds like you're just wanting to use a variable... Quote Link to comment https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/#findComment-797260 Share on other sites More sharing options...
DssTrainer Posted March 30, 2009 Author Share Posted March 30, 2009 actually it sounds like i have a simple string with a single digit in between 2 apostrophes Maybe I should make it simpler for you: I have a phrase "my_number = '4'" and sometimes its "my_number = '43'" and sometimes its "my_number = '0'" but regardless of what the resultant string is, I want to change it to "my_number = '1'". So regardless of the surrounding syntax or code or anything else, I am just looking for the answer to the question. -EDIT- Just because you think it is a PHP question, the fact is it is still a regex question, so don't move the thread. Quote Link to comment https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/#findComment-797315 Share on other sites More sharing options...
MadTechie Posted March 30, 2009 Share Posted March 30, 2009 $dig = 2; $data = "select * from table where language_id = '5' and date = now()"; $data = preg_replace('/\'\d\'/i', "\'$dig\'", $data); echo $data; Edit: (fix) oops hard coded a 2 in the regex! Quote Link to comment https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/#findComment-797336 Share on other sites More sharing options...
DssTrainer Posted March 31, 2009 Author Share Posted March 31, 2009 $dig = 2; $data = "select * from table where language_id = '5' and date = now()"; $data = preg_replace('/\'\d\'/i', "\'$dig\'", $data); echo $data; Edit: (fix) oops hard coded a 2 in the regex! that'll work It was much simpler than my attempts at it. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/#findComment-797415 Share on other sites More sharing options...
.josh Posted March 31, 2009 Share Posted March 31, 2009 well I'm glad you got your problem solved. If you had worded it that way in the first place, you would have gotten an answer a lot sooner. Your 2nd explanation "sounds" simpler to you because you know the context of your situation. We don't. We aren't psychic. Quote Link to comment https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/#findComment-797649 Share on other sites More sharing options...
.josh Posted March 31, 2009 Share Posted March 31, 2009 btw I'd probably make that regex more like this: $data = preg_replace("language_id = '\d+'", "language_id = '$dig'", $data); That other regex will change any 1 digit number between single quotes. This will change any 1+ digit number between quotes that's preceded by 'language_id = ' If your numbers will always be single digits and that will be the only thing in your string with a digit, then the previous one will be okay. Quote Link to comment https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/#findComment-797653 Share on other sites More sharing options...
Daniel0 Posted March 31, 2009 Share Posted March 31, 2009 http://catb.org/~esr/faqs/smart-questions.html#id307781 (read the rest of that page as well) Quote Link to comment https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/#findComment-797694 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.