Mostafa Talebi
Mostafa Talebi

Reputation: 9183

I need a RegExp to only match Latin characters and not else

I have written the following code to check if the given string is Latin or it contains some other non-latin chars like Persian. The issue is that it always returns true for both of the following strings:

$str = "Hello, What's up?"

Or

$str = "Hello, سلام"

While for the second string it should return false since it contains Persian characters (non-latin) too.

$default_rule = "/[a-zA-Z0-9\(\)\*_\-\!\#\$\%\^\&\*\,\.\"\'\]\[]*/";
    $rule  = ($rule==null) ? $default_rule : $rule;

    if(preg_match($rule, $str)==true)
    {
        // always returns true
    }

Upvotes: 3

Views: 8224

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149020

Your pattern will return true if the string contains zero or more of those characters you've specified. In other words, it will return true for any string at all. You need to put start (^) and end ($) anchors around it. Also you don't need to escape most of those characters (the character class causes them to be treated as literal characters):

$default_rule = '/^[a-zA-Z0-9()*_\-!#$%^&*,."\'\][]*$/';

But, this will match an empty string. To also make sure that the string is not empty, use the + quantifier (one or more) instead of *.

$default_rule = '/^[a-zA-Z0-9()*_\-!#$%^&*,."\'\][]+$/';

Upvotes: 8

Related Questions