Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/151815-solved-simple-preg_replace-escapes-me/
Share on other sites

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

 

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.

$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!

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.

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.

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.