Forum Moderators: coopster

Message Too Old, No Replies

Simple preg match regex question

Preventing multiple spaces

         

max4

8:12 am on Jul 2, 2009 (gmt 0)

10+ Year Member



I am trying to create a regular expression that allows multiple number groups with one space in between them, an example would be:

111 222 333 444... etc.

I have created the following expressions, but they don't work properly:

[fixed]
/^[0-9\ ]+$/
/^([0-9]+)[\s]([0-9]+)$/
[/fixed]

The first allows multiple spaces between numbers, and the second doesn't seem to work at all. As the title suggests, the goal is to allow only one space between the number groups.

Any suggestions? Thanks.

max4

8:47 am on Jul 2, 2009 (gmt 0)

10+ Year Member



I gave too little information, but regardless of this; I solved the problem.

g1smd

8:59 am on Jul 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The answer was... ?

max4

6:40 pm on Jul 3, 2009 (gmt 0)

10+ Year Member



The answer would not make sense without the full code, but since you're curious enough to ask; I simply removed the space from the preg_match() function which now reads:
[fixed]
preg_match("/^[0-9]+$/",$variable)
[/fixed]

This is used in a larger function which validates information as it is passed through an array. Because I have set the function to include the spaces in between when formatting the array, removing the space from the preg_match() function was the only way to ensure the data being passed is not tainted. (You can never be too extreme with regards to PHP security).

Peace,
Mohamed