Page 1 of 1
RegEx Help
Posted: Fri Jan 05, 2007 7:12 pm
by aliasxneo
Code: Select all
$sql = preg_replace("/\'(\d)\'/", "$1", $sql);
I'm trying to replace integers that have single quotes around them with the original integer. All you need to focus on here is the regular expression:
The problem is that it only replaces one digit integers, and not anything bigger then that. How do I make it so that it accepts any length of integer?
E.G The above expression works for:
'1'
But not for
'10'
Because there's two digits and I only specified one. Thanks.
Cheers,
- Josh
Posted: Fri Jan 05, 2007 7:24 pm
by georgeoc
After a character you can add a
* symbol to mean "any number", or a
+ symbol to mean "at least 1". You can also do
{3} to mean "exactly 3" or
{2, 6} to mean "between 2 and 6".
So, to answer your question, try this:
Code: Select all
$sql = preg_replace("/\'(\d+)\'/", "$1", $sql);
Posted: Fri Jan 05, 2007 7:26 pm
by Ollie Saunders
In regex you say [what to match][how many to match].
What to match can be anything you like.
How many can be:
* - 0 or more
+ - 1 or more (probably what you want)
{2,5} - between 2 and five
Btw you are escaping your single quotes when you don't need to.
Edit: Damn beaten to it

Posted: Fri Jan 05, 2007 8:59 pm
by feyd
Come now.. there's a board named "regex."
Posted: Fri Jan 05, 2007 9:36 pm
by aliasxneo
Thanks for the help, got it working. Cheers.