Jump to content

my expression works in RegEx Editor but not in php!!


RedMaster

Recommended Posts

Expression in question:

$regexp1 = "^(([a-zA-Z\s]+([\s]?[\-]?[\s]?)?)+([a-zA-Z]*))+$";

 

So I picked up Regular Expression Editor (version 1.4.0.0) from Waterpoof software and I made the above expression in it and tested it against the string below and it worked okay. However when dropping it into the function below it aparently returns false. I hate to make this a broad question but maybe you may see something I haven't yet.. The pupose of this expression is to validate names which may contain spaces and/or hyphens but to not allow users to get carried away with excessive spaces or hyphens. I am very new to regular expression btw so no flames please.

 

If you have any ideas I'd greatly appreciate it.

 

String:

cun lee pow-si- tu-do

 

Function:

function validateName($name){
$noodle1 = 0;
$regexp1 = "^(([a-zA-Z\s]+([\s]?[\-]?[\s]?)?)+([a-zA-Z]*))+$";
if (eregi($regexp1, $name) == true) {
$noodle1 = true; 
} else { 
$noodle1 = false; 
} 
return $noodle1;
}

Link to comment
Share on other sites

for

eregi use

$regexp1 = '^(([a-zA-Z[:space:]]+([[:space:]]?[-]?[[:space:]]?)?)+([a-zA-Z]*))+$';

 

for

Preg use

$regexp1 ='/^(([a-zA-Z\s]+([\s]?[\-]?[\s]?)?)+([a-zA-Z]*))+$/i';

 

as a side note

using eregi or adding i after the / delimitor on preg is case insensitive

so you can remove the A-Z and just have the a-z

 

 

example (this should work in replacment)

function validateName($name)
{
return preg_match('/^(([a-z\s]+([\s]?[\-]?[\s]?)?)+([a-z]*))+$/si', $name);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.