Regular expression for email address in Arabic

主宰稳场 提交于 2019-12-12 02:14:59

问题


EDITED

I googled to write a custom regular expression for my web application, and still I can't get what I wanted.

I want to check if a string passes this pattern or not:

*STRING*STRING INCLUDING ALL CHARS*STRING INCLUDING ALL CHARS#

for example:

*STRING*the first string تست یک*the second string تست دو#

Should return TRUE

*sdsdsd*the first string تست یکthe second string تست دو#

should return FALSE(because it's not the pattern as *STRING*STRING*STRING#)

$check = preg_match("THE RULE", $STRING);

I'm asking for THE RULE here, sorry if I asked my question in a wrong way...


回答1:


To check if a string has this pattern or not: *STRING*STRING*STRING#:

if (preg_match(
    '/^       # Start of string
    \*        # Match *
    ([^*]*)   # Match any number of characters except *
    \*        # Match *
    ([^*]*)   # Match any number of characters except *
    \*        # Match *
    ([^#]*)   # Match any number of characters except #
    \#        # Match #
    $         # End of string/x', 
    $subject, $matches))

Then use

filter_var($matches[1], FILTER_VALIDATE_EMAIL)

to check whether the first group might contain an e-mail address.




回答2:


No need for a regular expression, use filter_var():

function checkEmail($str){
    $exp = explode('*', $str);
    if(filter_var($exp[1], FILTER_VALIDATE_EMAIL) && $exp[2] && $exp[3] && substr($str, strlen($str)-1, strlen($str)) == '#') {
        return true;
    }
    return false;
}

$valid = checkEmail('*example@example.com*the first string تست یک*the second string تست دو#');


来源:https://stackoverflow.com/questions/12563547/regular-expression-for-email-address-in-arabic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!