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;
}

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);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.