Please help on this preg_match

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    Please help on this preg_match

    Hi,

    I am using the following regex to check for valid email
    addresses, but I am getting errors.

    Code:
    if (preg_match('\^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$\',$regex)){
    	   echo "VALID";
    	 }
    else {
         echo "INVALID";
    	}
    The errors that I get are:

    Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /home/guru54gt5/public_html/im/regex.php on line 6

    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/guru54gt5/public_html/im/regex.php on line 6

    Parse error: syntax error, unexpected '^', expecting ')' in /home/guru54gt5/public_html/im/regex.php on line 6

    Line 6 refers to the if stmt line in my script.

    I took the example from a tutorial and I thought that I understood everything, but I can not
    see why this is causing errors :)


    PS I changed the single quotes to double:
    if (preg_match("\^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$\",$regex )){

    and now I get just one error:
    Parse error: syntax error, unexpected T_STRING in /home/guru54gt5/public_html/im/regex.php on line 7

    But, again, I do not see why
    Thanks for any help.
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    #2
    OK,
    I have seen my mistake,
    I changed backslashes to forward slashes at the perimeters.
    Now it works OK

    BUT now I am confused by my results.

    I am using a simple form to input to this script:

    Code:
    <?php 
    if( isset($_POST['x_regex'])) {
       $regex = "{$_POST['x_regex']}";
    	 if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/",$regex)){
    	   echo "VALID";
    		 }
    		else {
    		  echo "INVALID";
    		}
    }
    else {
    require("regex_fm.php");
    }
    ?>

    When I enter this: fred@fgf.gh4.co
    It comes up as valid - but the number 4 should
    get it thrown out - why doesn't it ?

    and how can I make it do it ?

    Thanks

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      why should the number 4 get thrown out?

      Comment

      • jeddiki
        Contributor
        • Jan 2009
        • 290

        #4
        OK - you are correct - it shouldn't

        So that validation is OK

        Now I am also doing a URL validation:
        This is more complicated because I am using preg_replace

        I am trying to find a url withing the text and convert it to a
        hyper link.

        So I need to collect the parts of the url and stick them
        together

        First bit is to check to see if http or https
        If the http is not there I need to add it at the beginning of the result

        So my code is now

        Code:
        <?php 
        if( isset($_POST['x_regex'])) {
           $regex = "{$_POST['x_regex']}";
        	 $new_url = preg_replace("@((https?://)?([-\w]+\.)+(-\w{2,})+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)@", '<a href="$1">$1</a>', $regex);
        	  $new_url ="<a href=".$new_url."<a>"; 
        	 echo "<br><br>URL:  $new_url";
        	 echo "<br><br><a href=\"http://www.expert-world.com/im/regex.php\">Try Another<a>";
        	 exit;
        }
        else {
        require("regex_fm.php");
        }
        ?>
        I am a bit stuck.

        The testing page is here

        BTW - I got this validation from the a google search can
        someone tell me why this included: (:\d+)
        what does that colon do ?

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          You should probably take the time to actually teach yourself regex. A good learning tool is The Regex Coach. It lets you see how the regex works as you type it.

          Anyway, the colon is not a special character. It is a port number, like http://www.google.com:80/ (80 is the public port).

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            (80 is the public port)
            it is rather the default (HTTP) server port. if you have configured your server correctly, you can use any valid port you want.

            Comment

            Working...