Welcome to WebmasterWorld Guest from 107.20.20.39

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

check if a string contains a letter followed by a number

   
10:07 am on Sep 8, 2009 (gmt 0)

5+ Year Member



hi i have a variable called $username like this:

$username = $_POST['username'];

i want to be able to check to see if the value for $username starts with the letter p (not case sensative) and is followed by a numeric set of values. If so echo something else echo something else

can anyone tell me how i can achieve this?

thanks

10:17 am on Sep 8, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Just use [url=www.php.net/preg_match]preg_match[/url] to check for ^p\d+$ or ^p(\d+)$ if you want to work on the numeric part.
10:47 am on Sep 8, 2009 (gmt 0)

5+ Year Member



i've managed to do this:

if (!preg_match('/[[:^digit:]]/', $username)) { }

but this only detects if it is a number, how can i determine if it also begins with a letter p ?

12:00 pm on Sep 8, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



As I said, use ^p\d+$ as the regexp (that's [[:^digit:]] in your example) to check for that. the ^ matches at the beginning of the string, and $ at the end, so that'll let p123 through, but not p123a or ap123.
1:06 pm on Sep 8, 2009 (gmt 0)

5+ Year Member



thats great, works perfectly now.

thanks