2

I am new to PHP, and very new to Regular Expressions. I am currently trying to write a Regular Expression that makes sure the characters are all Alpha Numeric, and that accepts other characters a well. I would like to accept áéíñóúüÁÉÍÑÓÚÜ and @.+-. To accept all alphanumeric characters, and the symbols @.+-. I use the expression:

if (!preg_match("!^[\[email protected]]*$!")

Also, before I continue I would like to add. These are values being grabbed from a form input, and they should all be one word phrases (as in no spaces). Ok now lets continue! :D Now I want to grab the characters áéíñóúüÁÉÍÑÓÚÜ as well. When I use the expression:

if (!preg_match("/[\wáéíñóúüÁÉÍÑÓÚÜ@.\+-].*/")

It does not seem to work, could anyone please help? If you are confused, or it is worded bad just ask and I will rewrite it :)

UPDATE

My expression:

if (!preg_match("!^[\[email protected]]*$!")

does work, it is the other one that is not working. Thank You! :)

UPDATE 2

Now when I try the expression:

if (!preg_match("~^[\p{L}\p{N}@+._-]+$~", $email)) {

with the input of michael.jonesáéí@gmail.com it raises a error. There error it raises is "Email contains restricted characters." because I made it say that. Here is the specific code I am using:

<?php
if (!preg_match("~^[\p{L}\p{N}@+._-]+$~", $email)) {
?>
<div id="allinputboxerror" class="col-xs-12"> Email contains restricted characters.</div>
<?php } ?>  
                                                }

and here is all of the code I am using:

<?php
                                if ($_POST) {
                                    $emailtest1 = False;
                                    $emailtest2 = False;
                                    $emailtest3 = False;
                                    $emailtest4 = False;
                                    $emailtest5 = False;

                                    // Test #1 - Makes sure there is a input

                                    if(empty($_POST['email'])) {
?>
                                        <div id="allinputboxerror" class="col-xs-12"> Please enter your email. </div>
<?php
                                    }

                                    else {
                                        $emailtest1 = True;

                                        // Test #2 - Makes sure it does not already exist

                                        $usernamequery = "SELECT 1 FROM users WHERE email = :email";

                                        $usernameparams = array(':email' => $_POST['email']);

                                        try{
                                            $emailstmt = $connection->prepare($usernamequery);
                                            $emailresult = $emailstmt->execute($usernameparams);
                                        }

                                        catch(PDOException $ex){
                                            echo ("Failed to run query: " . $ex->getMessage());
                                        }

                                        $emailcolumns = $emailstmt->fetch();

                                        if($emailcolumns){
?>
                                            <div id="allinputboxerror" class="col-xs-12"> This email already exists. </div>
<?php
                                        }

                                        else {
                                            $emailtest2 = True;

                                            // Test #3 - Makes sure it fits the length requirements

                                            if(strlen($email) < 5 ) {
?>
                                                <div id="allinputboxerror" class="col-xs-12"> Email is to short. </div>
<?php
                                            }

                                            elseif(strlen($email) > 75 ) {
?>
                                                <div id="allinputboxerror" class="col-xs-12"> Email is to long. </div>
<?php
                                            }

                                            else {
                                                $emailtest3 = True;

                                                // Test #4 - Makes sure it does not have any restricted characters

                                                if (!preg_match("~^[\p{L}\p{N}@+._-]+$~", $email)) {
?>
                                                    <div id="allinputboxerror" class="col-xs-12"> Email contains restricted characters. </div>
<?php       
                                                }

                                                else {
                                                    $emailtest4 = True;

                                                    // Test #5 - Makes sure the email is valid

                                                    if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
?>
                                                        <div id="allinputboxerror" class="col-xs-12"> Not a valid email. </div>
<?php       
                                                    }

                                                    else {
                                                        $emailtest5 = True;

                                                        // Final Check

                                                        if (($emailtest1 = True) and ($emailtest2 = True) and ($emailtest3 = True) and ($emailtest4 = True) and ($emailtest5 = True)) {
                                                            // Email is valid! :D
                                                        }

                                                        else {
?>
                                                            <div id="allinputboxerror" class="col-xs-12"> There is a error. </div>
<?php
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }   
?>

1 Answer 1

2

Regex to accept all alphanumeric characters, and the symbols @.+-.

^[\p{L}\p{N}@+.-]+$

Your regex ^[\[email protected]]*$ won't match the accented characters. That is \w would match only the English alphabets, digits 0-9 plus the underscore symbol.

  • \p{L} matches any kind of letter from any language

  • \p{N} matches any kind of numeric character in any script.

  • + after the char class would repeat the previous token one or more times. I suggest you to use + instead of * because * repeats the previous token zero or more times. So it matches empty strings also.

DEMO

Update:

You ust need to include the unicode modifier u, so that it would make \p{L} to match accented characters.

if (!preg_match("~^[\p{L}\p{N}@+._-]+$~u", $email)) {
Sign up to request clarification or add additional context in comments.

13 Comments

I think this answer would be better if you also explained where the OP went wrong.
@ruakh i think i explained.
@Tom: I think you've misparsed the sentence you quote. Avinash Raj means "\w will only match certain kinds of letters from certain languages".
@ruakh He didn't wrote that it matches some/certain letter. He wrote that it won't match any letter.
@ruakh you're correct. I need to improve my writting skills.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.