User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 428,194 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,151 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting

PHP Split function

Join Date: May 2008
Posts: 31
Reputation: rgviza is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
rgviza rgviza is offline Offline
Light Poster

Re: PHP Split function

  #3  
May 15th, 2008
The cleanest way to handle this is:
$id='ha12345';

switch(true)
{
  case preg_match("/^\w{2}\d{5}$/",$id):
         //user is LLNNNNN
         echo "LLNNNNN";
         break;
  case preg_match("/^\w{3}$/",$id):
         //user is LLL
         echo "LLL";
         break;
  default:
         //put login error here. use "username or password" is wrong so they
         //can't brute-guess your id format or ids. You also don't want to mention
        //"incorrect format".
}

For lots of regex goodness go here for tutorial. It's for .Nut but regex works the same way for any perl compatible regex and this tutorial is really easy to understand compared to others.

Insight into this particular regex.
each significant part is followed by the explanation in brackets:
/^[from start of string]\w{2}[expect 2 word characters]\d{5}[then 5 numeric digits]$[then end of string]/

This way 1234-ha12345-3242lkj won't match, even though there is a LLNNNNN substring in it.

preg_match returns boolean so you can use it wherever true or false will trip a condition, like in an if, or while, whatever. It is very flexible and powerful but not the most efficient.
Last edited by rgviza : May 15th, 2008 at 2:52 pm.
Reply With Quote  
All times are GMT -4. The time now is 3:04 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC