Web Analytics Made Easy -
StatCounter PHP ereg to preg_match problem - CodingForum

Announcement

Collapse
No announcement yet.

PHP ereg to preg_match problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP ereg to preg_match problem

    Following code has written using ereg Please re-write following code using preg_match
    I was unable to add delimiters as all the types of delimiters available inside the code.

    Please re-write this code:

    PHP Code:
    function c_email($email) {
        if (
    ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$'$email)) {
            return 
    1;} else {
            return 
    0;}


  • #2
    We don't write code on demand. Make an attempt and we will help you if you're stuck.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    Make A Donation https://www.paypal.me/KevinRubio

    Comment


    • #3
      That expression will reject most email addresses. If you are going to use a regular expression to validate email addresses at least use the correct expression.

      There have been plenty of examples of email validation scripts posted on this forum - all you need to do is search for them.
      Stephen
      Learn Modern JavaScript - http://javascriptexample.net/
      Helping others to solve their computer problem at http://www.felgall.com/

      Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

      Comment


      • #4
        PHP Code:
        <?php

        function c_email($email) {
            return 
        filter_var($emailFILTER_VALIDATE_EMAIL) !== false;
        }

        Comment


        • #5
          Originally posted by Jacques1 View Post
          PHP Code:
          <?php

          function c_email($email) {
              return 
          filter_var($emailFILTER_VALIDATE_EMAIL) !== false;
          }
          Thanks for helping!

          Comment


          • #6
            Just be warned that neither the regex nor filter_var check for valid LENGTHS -- only if the characters used are valid -- and I like to check for if the domain has a MX or a A assigned to it.

            This is an even more complete check that an acquaintance and I came up with on a long-defunct forum like a decade ago:

            Code:
            function isValidEmail($address) {
            	if (filter_var($address, FILTER_VALIDATE_EMAIL) == FALSE) return false;
            	
            	/* explode out local and domain */
            	list($local, $domain) = explode('@', $address);
            	
            	$localLength = strlen($local);
            	$domainLength = strlen($domain);
            	
            	return (
            		/* check for proper lengths */
            		($localLength > 0 && $localLength < 65) &&
            		($domainLength > 3 && $domainLength < 256) && (
            			checkdnsrr($domain,'MX') ||
            			checkdnsrr($domain,'A')
            		)
            	);
            }
            It's probably the most robust check you can use as it checks for both proper lengths of the local and domain (something filter_var and regex fail to do) as well as using checkdnsrr to make sure the domain can actually accept mail.

            Some people will reject the A check fallback on the grounds that any legitimate mail server should have a MX, but I've found there are too many poorly configured servers who use their A record instead of MX.

            Laughably, rejecting A record only mail addresses is one of the things that's made Microsoft Live mail less useful than it used to be back when it was called Hotmail.
            Walk the dark path, sleep with angels, call the past for help.
            https://cutcodedown.com
            https://medium.com/@deathshadow

            Comment

            Working...
            X