• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

regular expressions question v.php

D1G1T4L

[H]ard|Gawd
Joined
Feb 1, 2001
Messages
1,212
I have a phone field and the way I want to store in the database is just digits.
example: 4154065623

I need a regular expression that will remove spaces/dashes/etc

so for example if a user puts something like
(415)406-56 23 it will turn it into 4154065623

using ereg_replace function I am guessing...

EDIT:

never mind.. I have figured it out on my own

$phone = ereg_replace ('[^0-9]', '' , $phone);
 
$data = "(415)406-56 23";
$filtered = preg_replace("/[^0-9]/","",$data);

That won't help if the user inputs too little or too many numbers though.
 
You shouldn't use ereg_replace(), it's been deprecated a good while now.
 
^(?:(?:[\+]?(?<CountryCode>[\d]{1,3}(?:[ ]+|[\-.])))?[(]?(?<AreaCode>[\d]{3})[\-/)]?(?:[ ]+)?)?(?<Number>[a-zA-Z2-9][a-zA-Z0-9 \-.]{6,})(?:(?:[ ]+|[xX]|(i:ext[\.]?)){1,2}(?<Ext>[\d]{1,5}))?$

:D
 
^ smilies ftl. looks like someone didn't want to use an xml parser :p
 
You don't need a regex for this. Just replace spaces and dashes with an empty string.

PHP has str_replace(), so

Code:
str_replace( array('() -'), '', $yourVariable )

... yields what you want.
 
You don't need a regex for this. Just replace spaces and dashes with an empty string.

PHP has str_replace(), so

Code:
str_replace( array('() -'), '', $yourVariable )

... yields what you want.

I'm not sure that is the best idea since you could have bad actors that put "1-800-EAT-SHIT" or something. Regex is just more reliable.

Of course, I have to assume the database field being used is setup to only accept numbers so it probably would just generate an error if they did.
 
I'm not sure that is the best idea since you could have bad actors that put "1-800-EAT-SHIT" or something. Regex is just more reliable.
It's not more reliable; it's just less specific. If the OP has things other than dashes and parens that he wants to filter out, it's easy enough to do using str_replace(). And if the list gets long -- to the point where being exclusive is easier -- then it's trivial to write a loop that copies only digits from the input value. I retain my opinion that regex is more than what's needed here.
 
I am sure you are correct about the effectiveness of the code and given the exact params the OP put stated, that should work. Hell I don't know enough of PHP to even disagree about ease of use, but since he specifically mentioned he only wants numbers, I think your second idea would be safer because it is more of a white hat type solution - only take what you want.

Never trust user data.
 
Back
Top