homepage Welcome to WebmasterWorld Guest from 72.44.48.122
register, login, search, subscribe, help, library, PubCon, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library : Charter : Moderators: coopster & eelixduppy & 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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
WebmasterWorld ® and PubCon ® are a Registered Trademarks of Pubcon Inc.
© Pubcon Inc. 1996-2012 all rights reserved