【问题标题】:PHP regex question : how to match none-ascii letters in latin1_swedish_ci charset?PHP 正则表达式问题:如何匹配 latin1_swedish_ci 字符集中的非 ascii 字母?
【发布时间】:2010-04-07 04:46:22
【问题描述】:

我有这个字符串:我认为是德语的 Verbesserungsvorschläge。现在我想将它与 php 中的正则表达式匹配。更笼统地说,我想匹配像德语这样的字符,它们在 ASCII 集中不是 100%。

谢谢。

【问题讨论】:

    标签: php regex unicode


    【解决方案1】:

    如果您使用 8 位字符集,则正则表达式 [\x80-\xFF] 匹配任何非 ASCII 字符。在 PHP 中是:

    if (preg_match('/[\x80-\xFF]/', $subject)) {
      # String has non-ASCII characters
    } else {
      # String is pure ASCII or empty
    }
    

    【讨论】:

      【解决方案2】:
      preg_match_all('~[^\x00-\x7F]~u', 'Verbesserungsvorschläge', $matches);
      

      【讨论】:

        【解决方案3】:

        这是一个痛苦的世界,但您可以尝试使用十六进制值,如“/Verbesserungsvorschl\xc3ge/”中的简单扩展字符。

        可以在表格中找到十六进制值,以便使用

        即时确定
        echo dechex( ord( ä ) ); 
        

        对于完整的 unicode,您可以使用 /u 作为修饰符。请参阅http://www.php.net/manual/en/regexp.reference.unicode.php 和其他页面。我的理解是 unicode 在 PHP 版本 6 中会更好地工作。

        【讨论】:

        • 感谢 unicode 链接。不幸的是,就我而言,字符集位于 latin1_swedish_ci 中。
        猜你喜欢
        • 2013-09-22
        • 2011-01-08
        • 1970-01-01
        • 1970-01-01
        • 2011-01-09
        • 1970-01-01
        • 2011-08-29
        • 2013-02-19
        • 1970-01-01
        相关资源
        最近更新 更多