Welcome to WebmasterWorld Guest from 54.144.44.9

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

What am I doing Wrong with this regex

Regular Expression Trouble

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

Junior Member

10+ Year Member

joined:Mar 26, 2005
posts:148
votes: 0


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)

Junior Member

10+ Year Member

joined:Apr 17, 2006
posts:195
votes: 0


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)

Junior Member

10+ Year Member

joined:Mar 26, 2005
posts:148
votes: 0


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)

Administrator

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

joined:July 31, 2003
posts:12541
votes: 1


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)

Junior Member

10+ Year Member

joined:Feb 5, 2006
posts:69
votes: 0


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)

New User

5+ Year Member

joined:Nov 2, 2006
posts:4
votes: 0


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)

Junior Member

10+ Year Member

joined:Jan 28, 2006
posts:96
votes: 0


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. :)