fifin04 Posted February 11, 2010 Share Posted February 11, 2010 Hi there.. I really need help with my ugly regex code because it still can't diferrentiate string and string contain only numbers ok here the scenario; All the string must meet this 3 conditions 1.String contain only alpha,space and dot 2.String contain only alphanumeric,space and dot 3.String contain only numbers so here's my attempt code so far $testStringArray = array("abc. bc. def83","abc. bc.","123","567","fdg. qwe.","dfg. rtu. hu8"); //so i start filter testing here $total = count($testStringArray); for($i=0;$i<$total;$i++){ if(preg_match('/[a-zA-Z.\...\\s.]+$/',$testStringArray[$i])){ echo $i." : ".$testStringArray[$i]." contain only alpha,dot,space"; }else if(preg_match('/^[a-zA-Z0-9.\...\\s.]+$/i/',$testStringArray[$i])){ echo $i." : ".$testStringArray[$i]." contain only alpha,dot,numbers,space"; }else if(preg_match('/^[0-9]/',$testStringArray[$i])){ echo $i." : ".$testStringArray[$i]." contain only numbers"; } } Current output: contain only alpha,dot,numbers,space contain only alpha,dot,space contain only alpha,dot,numbers,space-->it seems the regex not wokr here contain only alpha,dot,numbers,space-->it seems the regex not wokr here contain only alpha,dot,space contain only alpha,dot,numbers,space suppose to : contain only alpha,dot,numbers,space contain only alpha,dot,space contain only numbers--->shoud be numbers only contain only numbers--->shoud be numbers only contain only alpha,dot,space contain only alpha,dot,numbers,space So i really need help with it....thanks in advance guys... Link to comment https://forums.phpfreaks.com/topic/191740-need-help-with-preg_match/ Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 I think it fails because the order of 'if's for example take string "123" U aks "Does this string contains only alpha space or dot characters"? NO Then u ask "Does this string contains alpha numbers space or dot chars only"? YES cuz "123" realy cointains numbers And since second's 'IF' answer was YES u never will reach third 'IF' so try do something like: if('/^[0-9]+$/im') //only numbers { } else if ('/^[a-z\\s\\.]+$/im') //alpha space dot { } else if('/^[a-z0-9\\s\\.]+$/im') //alpha numbers space dor { } Link to comment https://forums.phpfreaks.com/topic/191740-need-help-with-preg_match/#findComment-1010670 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.