homepage Welcome to WebmasterWorld Guest from 54.205.247.203
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
check if a string contains a letter followed by a number
ahmed24




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

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

 

janharders




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

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.

ahmed24




msg:3985924
 10:47 am on Sep 8, 2009 (gmt 0)

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 ?

janharders




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

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.

ahmed24




msg:3985951
 1:06 pm on Sep 8, 2009 (gmt 0)

thats great, works perfectly now.

thanks

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved