Author |
Message |
scorpious
Worker


Joined: Dec 03, 2005
Posts: 153
|
Posted:
Fri Jan 20, 2012 2:04 pm |
|
Hi All
I have an input box for members name, it works ok
I have the following code
Code: $mnane = preg_replace("/[^a-zA-Z0-9\s]/", "", $mnane);
|
It work ok, till someone puts a members name in with underscore _ within it, ie. com_thor the code removes the underscore and joins the name together.
I also have a Location input, which is 40 Characters, when we put in a location then use a comma to seperate town name with area ie. birmingham, west midlands it removes the comma.
How can I allow the use of underscore and comma's within the code
Cheers
Scorp |
|
|
|
 |
hicuxunicorniobestbuildpc
The Mouse Is Extension Of Arm

Joined: Aug 13, 2009
Posts: 1123
|
Posted:
Fri Jan 20, 2012 3:02 pm |
|
I guess if you must use \\ for all your caracters you wanna add to show username with underscore. Try this one.
Code:$mnane = preg_replace("/[^a-zA-Z0-9_-\\[\\]\\{\\}\\=-]/", "", $mnane);
|
|
|
|
|
 |
scorpious

|
Posted:
Fri Jan 20, 2012 5:00 pm |
|
Hi unicornio
I dont use \\ and the code did not work, however, if I use this code:
Code: "/[^a-zA-Z0-9_-\s]/", ""
|
The underscore works, Its just the commas I need to allow now.
Cheers scorp |
|
|
|
 |
hicuxunicorniobestbuildpc

|
Posted:
Fri Jan 20, 2012 5:58 pm |
|
Code: /[a-zA-Z0-9,]+/ matches if any of the characters are alphanumeric + comma.
/^[a-zA-Z0-9,]+$/ matches if all of the characters are alphanumeric + comma.
|
Code: $mnane = preg_replace("/^[a-zA-Z0-9,]*$/", "", $mnane);
|
Code:
/ : regex delimiters.
^ : start anchor
[..] : Char class
0-9 : any digit
a-z : any alphabet
, : a comma. comma is not a regex metachar, so you need not escape it
+ : quantifier for one or more. If an empty input is considered valid, change + to *
$ : end anchor
i : to make the matching case insensitive.
|
Try this one and let me know. If it doesn't work then someone else can help u
 |
|
|
|
 |
scorpious

|
Posted:
Fri Jan 20, 2012 6:25 pm |
|
Hi unicornio
I have just added the comma within the code:
Code:"/[^a-zA-Z0-9,_-\s]/", ""
|
And now I can use the comma and underscore
The code you shown:
Code:
/ : regex delimiters.
^ : start anchor
[..] : Char class
0-9 : any digit
a-z : any alphabet
, : a comma. comma is not a regex metachar, so you need not escape it
+ : quantifier for one or more. If an empty input is considered valid, change + to *
$ : end anchor
i : to make the matching case insensitive.
|
I have been looking around the net for a explanation to preg_replace and the breakdown of it, that code explains, nice one
Cheers Scorp |
|
|
|
 |
hicuxunicorniobestbuildpc

|
Posted:
Fri Jan 20, 2012 7:07 pm |
|
I'm glad you solved it. It was my pleasure to help u. See u around. |
|
|
|
 |
montego
Site Admin

Joined: Aug 29, 2004
Posts: 9457
Location: Arizona
|
Posted:
Sat Jan 21, 2012 9:03 am |
|
RN 2.5.0 uses the following instead (takes Unicode into account):
/[^\p{L}\p{N}\p{Pd}\p{Pc}]/
This translates to:
Any Letter
Any Number
Any dash separator (e.g., "-")
Any connector (e.g., "_") |
_________________ Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! |
|
|
 |
|