doufu8887 2015-08-27 12:01
浏览 53
已采纳

php mb_ereg_match上的例外

I am using mb_ereg_match to validate that a domain name does not containe illegal characters.

I am using this regex:

'/:\/\/|www[.][a-zA-Zα-ωΑ-ΩάέύήίόώϋϊΐΰΆΈΏΊΎΌΉΫΪÀàÂâÆæÄäÇçÉéÈèÊêËëÎîÏïÔôŒœÖöÙùÛûÜüŸÿ0-9]+[.]|^[-]+|^[.]+|[-]+$|[.]+$|[-]{2,}|[.]{2,}|[^\w-.]|-[.]|[.]-/u'

Which as you can se by your self contain all the basic latin chars, nums, France's letters and the whole Greek alphabet.

My validation code is the following:

$utf8 = (mb_detect_encoding($value) == 'UTF-8') ? TRUE : FALSE;

if ($utf8){
    mb_internal_encoding('UTF-8');
    mb_regex_encoding('UTF-8');
    $matches = mb_ereg_match($pattern, $value);
}else{
    preg_match($pattern, $value, $matches);
}

I am trying to validate this:

'geoσσσrge.cσσσσσm.gr'

Here is the error I get:

mb_ereg_match(): mbregex compile err: empty range in char class

The error does not appear all the time. Usually it apears when it stays idle for a long time and after I refresh my page returns to normal.

I don't know how to handle this error or how to approche it in order to find the source of the problem.

Any suggestions?

  • 写回答

1条回答 默认 最新

  • douba9425 2015-08-27 12:28
    关注

    \w through . is not a range it can understand. Escape the - or move the - to the start; [^\w-.].

    $pattern = '/:\/\/|www[.][a-zA-Zα-ωΑ-ΩάέύήίόώϋϊΐΰΆΈΏΊΎΌΉΫΪÀàÂâÆæÄäÇçÉéÈèÊêËëÎîÏïÔôŒœÖöÙùÛûÜüŸÿ0-9]+[.]|^[-]+|^[.]+|[-]+$|[.]+$|[-]{2,}|[.]{2,}|[^\w\-.]|-[.]|[.]-/u';
    $value = 'geoσσσrge.cσσσσσm.gr';
    
    $utf8 = (mb_detect_encoding($value) == 'UTF-8') ? TRUE : FALSE;
    
    if ($utf8){
        mb_internal_encoding('UTF-8');
        mb_regex_encoding('UTF-8');
        $matches = mb_ereg_match($pattern, $value);
    }else{
        preg_match($pattern, $value, $matches);
    }
    

    or

    $pattern = '/:\/\/|www[.][a-zA-Zα-ωΑ-ΩάέύήίόώϋϊΐΰΆΈΏΊΎΌΉΫΪÀàÂâÆæÄäÇçÉéÈèÊêËëÎîÏïÔôŒœÖöÙùÛûÜüŸÿ0-9]+[.]|^[-]+|^[.]+|[-]+$|[.]+$|[-]{2,}|[.]{2,}|[^-\w.]|-[.]|[.]-/u';
    $value = 'geoσσσrge.cσσσσσm.gr';
    
    $utf8 = (mb_detect_encoding($value) == 'UTF-8') ? TRUE : FALSE;
    
    if ($utf8){
        mb_internal_encoding('UTF-8');
        mb_regex_encoding('UTF-8');
        $matches = mb_ereg_match($pattern, $value);
    }else{
        preg_match($pattern, $value, $matches);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了