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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题