Jump to content

preg_match in function adds 0 or 1. Why?


Balderick

Recommended Posts

I have a thing I dont get.

 

I found this regex part to check for correct subdomains.

       function check_subdomain($data){
	     
		 if (!empty($data)){
	         $data = preg_match("/^(?:[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9]|[A-Za-z0-9])$/", $data);
			return($data);
			
		} 
		else { }
      }

      echo check_subdomain($data);

but the strange thing is that echo check_subdomain($data) outputs the input and adds a 0 when wrong and a 1 when correct.

 

why is this done and how to avoid it?

 

 

Link to comment
Share on other sites

That's not all of your code. I suspect you're printing out $data somewhere else.

 

You have an unnecessary else statement, just remove that from your code.

 

You can simplify your function to just this:

function check_subdomain($data){
  return preg_match("/^(?:[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9]|[A-Za-z0-9])$/", $data);
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...