dqudtskm49788 2013-02-14 14:28
浏览 56
已采纳

浏览器版本匹配 - PHP 5.1.6中的正则表达式中断

I'm using a script to detect the browser and its version that is referenced multiple times on the get_browser page on php.net.

The file containing the script is saved as UTF-8 and actually it works fine´:

// relevant parts only where $ub stores the browser name like "Safari"
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
preg_match_all($pattern, $userAgent, $matches);

Now we're running a site on a server that uses PHP 5.1.6. Unfortunately it breaks the regex and displays this warning:

PHP Warning: preg_match_all() [function.preg-match-all]: Compilation failed: unrecognized character after (?< at offset 3 in /file.php on line 48

How must the pattern be updated to work properly with PHP 5.1.6?

  • 写回答

1条回答 默认 最新

  • dt246813579 2013-02-14 14:34
    关注

    The syntax you are trying to use isn't introduced until PHP 5.2.2. (?<name>pattern). You should be able to use this instead (?P<name>pattern).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程