Welcome to WebmasterWorld Guest from 54.161.255.61

Forum Moderators: coopster & jatar k

What am I doing Wrong with this regex

Regular Expression Trouble

   
8:24 pm on Nov 1, 2006 (gmt 0)

10+ Year Member



I'm just trying to check and see if this name is valid. I submit things like "12345" and it evaluates to true...

function checkName ($name)
{
$status = FALSE;
$pattern = "/^[A-Za-z]+$/";
if (preg_match($pattern, $name));
$status = TRUE;

return $status;

}

What am I doing wrong?
--Nick

8:40 pm on Nov 1, 2006 (gmt 0)

5+ Year Member



Hi! You have ; in the last char of the IF condition, remove it.


if (preg_match($pattern, $name))
$status = true;
8:44 pm on Nov 1, 2006 (gmt 0)

10+ Year Member



Son............of.............a.............B
I would have never gotten that.....note to self: use debugger next time.
Thanks a million.
--Nick
4:57 pm on Nov 2, 2006 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Another good reason to always use curly braces ;)
if (preg_match($pattern, $name)) { 
$status = TRUE;
}
Syntax parser would have caught that right away.


You are strongly encouraged to always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.

[pear.php.net...]

Sound advice!

7:32 am on Nov 4, 2006 (gmt 0)

5+ Year Member



There's no reason to use a breakline, though. The entire reason to allow a conditional without curly braces is so you can keep it on one line:

if (preg_match($pattern, $name)) $status = TRUE;

That's actually more legible than with curly braces.

11:10 am on Nov 4, 2006 (gmt 0)

5+ Year Member



I think it really depends on what you are used to, and the method that you know best, or that fits your coding style better. I prefer to use curly brackets, because that is how I learned, and the method I like best.

If you are a beginner, though, you could technically use either method, but I believe the curly braces is the better, easier, easier to read method. IMHO.

8:26 pm on Nov 4, 2006 (gmt 0)

5+ Year Member



Seeing as nobody else has mentioned it, you could shorten that function quite a bit:

function checkName ($name)
{
return preg_match('/^[A-Za-z]+$/', $name);
}

Or, if you want to ensure checkName returns a real boolean, rather than the 0 or 1 preg_match returns:

  • return (bool)preg_match('/^[A-Za-z]+$/', $name);

    using casting [us3.php.net]
  • return !!preg_match('/^[A-Za-z]+$/', $name);

    the double-not trick, which is just like casting to bool
  • return preg_match('/^[A-Za-z]+$/', $name) ? true : false;

    the ternary operator [us3.php.net]
  • return preg_match('/^[A-Za-z]+$/', $name) == 1;

    making use of preg_match's returning 1 or 0

But that's just style, and your preferences undoubtedly vary from my own. :)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month