Jump to content

Need help with preg_match


fifin04

Recommended Posts

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
Share on other sites

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