Jackie Honson
Jackie Honson

Reputation: 1459

Allow a-z, A-Z, 0-9, space and -. preg_match

How can I allow a-z, A-Z, 0-9, - and space with preg_match.

I currently have the following:

if (!preg_match("/^[_a-zA-Z0-9]+$/", $name))

Also a side-question, does any of you know a good guide to learn preg_match.

Upvotes: 1

Views: 24584

Answers (4)

Hasan Yilmaz
Hasan Yilmaz

Reputation: 101

only [a-z, A-Z, 0-9 and Space] allow. We delete the first space with LTRIM.

    <?php
$name = "Hasan Yİlmaz SÄ°MFER "; 
if(ltrim(preg_match('/^[a-zA-ZığüşöçİGÜŞÖÇ0-9- ]+$/', $name2), ' '))
    {
        echo $name;
    }

?>

Upvotes: 0

Rushi Adhia
Rushi Adhia

Reputation: 11

Using /^[a-zA-Z'- ]+$/ Shows the below given warning

"preg_match(): Compilation failed: range out of order in character class at offset 10"

Instead of the above-mentioned code, you can use /^[a-zA-Z' -]+$/. This will not show the warning.!

Upvotes: -2

Shahid Latif
Shahid Latif

Reputation: 135

I think this will work for you

    <?php
    $result = preg_match("/^[a-zA-Z0-9 \s]+$/", $name));
    ?>

Upvotes: 2

sourcecode
sourcecode

Reputation: 1802

just give space and "-" in your brackets [ ]
example

<?php
$name = "A- "; 
if(preg_match("/^[_a-zA-Z0-9- ]+$/", $name))
{
echo "hello";
}
?>

and a good site for studying : link

Upvotes: 1

Related Questions