Jump to content
Sign in to follow this  
Balderick

preg_match in function adds 0 or 1. Why?

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?

 

 

Share this post


Link to post
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);
}

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...