WordPress.org

Ready to get started?Download WordPress

Ideas

A safer and shorter function for "is_email"

  1. iChrisNL
    Member

    12345

    Ok, so after checking the "formatting.php" file I discovered the function is_email($user_email) (Line 1280).

    This can easily be done much quicker, and safer.

    Current function (including it's comments)

    /**
     * Checks to see if the text is a valid email address.
     *
     * @since 0.71
     *
     * @param string $user_email The email address to be checked.
     * @return bool Returns true if valid, otherwise false.
     */
    function is_email($user_email) {
    	$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
    	if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) {
    		if (preg_match($chars, $user_email)) {
    			return true;
    		} else {
    			return false;
    		}
    	} else {
    		return false;
    	}
    }

    Probably better

    /**
     * Checks to see if the text is a valid email address.
     *
     * @since 0.71
     *
     * @param string $user_email The email address to be checked.
     * @return bool Returns true if valid, otherwise false.
     */
    function is_email($user_email) {
    	if(filter_var($user_email, FILTER_VALIDATE_EMAIL))
            {
                    return true;
            }
            else
            {
                    return false;
            }
    }
    Posted: 5 years ago #
  2. Shinyshadow
    Member

    function is_email($user_email) {
    	return filter_var($user_email, FILTER_VALIDATE_EMAIL);
    }
    Posted: 5 years ago #
  3. That's a great example of something that could be considered once we require PHP 5.2+, but that will be a while.

    http://php.net/filter_var

    Posted: 4 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    12345
    5 Votes
  • Status

    Sorry, not right now