preg_match 和(非英语)拉丁字符?

preg_match and (non-English) Latin characters?(preg_match 和(非英语)拉丁字符?)
本文介绍了preg_match 和(非英语)拉丁字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XHTML 表单,我要求人们在其中输入他们的全名.然后我使用以下模式将其与 preg_match() 匹配: /^[p{L}s]+$/

I have a XHTML form where I ask people to enter their full name. I then match that with preg_match() using this pattern: /^[p{L}s]+$/

在我运行 PHP 5.2.13 (PCRE 7.9 2009-04-11) 的本地服务器上,这工作正常.在运行 PHP 5.2.10 (PCRE 7.3 2007-08-28) 的网络主机上,当输入的字符串包含丹麦拉丁字符 ø ( http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=%F8&mode=char ).

On my local server running PHP 5.2.13 (PCRE 7.9 2009-04-11) this works fine. On the webhost running PHP 5.2.10 (PCRE 7.3 2007-08-28) it doesn't match when the entered string contains the Danish Latin character ø ( http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=%F8&mode=char ).

这是一个错误吗?有解决办法吗?

Is this a bug? Is there a work around?

先谢谢你!

推荐答案

所以,问题正如推测的那样.您没有使用 /u 修饰符.这意味着 PCRE 不会查找 UTF-8 字符.

So, the problem is as presumed. You are not using the /u modifier. This means that PCRE will not look for UTF-8 characters.

无论如何,应该这样做:

In any case, this is how it should be done:

var_dump(preg_match('/^[p{L}s]+$/u', "ø")); 

并且适用于我的所有版本.其他人可能存在错误,但这里不太可能.

And works on all my versions. There might be a bug in others, but that's not likely here.

你的问题是这也有效:

var_dump(preg_match('/^[p{L}s]+$/', utf8_decode("ø")));

请注意,这里使用 ISO-8859-1 而不是 UTF-8,并省略了 /u 修饰符.结果是int(1).显然 PCRE 在非 /u nicode 模式下将 Latin-1 ø 解释为匹配 p{L}.(大多数单字节xA0-xFF 是Latin-1 中的字母符号,8 位代码点与Unicode 中的相同,所以实际上没问题.)

Notice that this uses ISO-8859-1 instead of UTF-8, and leaves out the /u modifier. The result is int(1). Obviously PCRE interprets the Latin-1 ø as matching p{L} when in non-/unicode mode. (Most of the single-byte xA0-xFF are letter symbols in Latin-1, and the 8-bit code point as the same as in Unicode, so that's actually ok.)

结论:您的输入实际上是 ISO-8859-1.这就是为什么它在没有 /u 的情况下意外地为您工作的原因.改变这一点,并精确地输入字符集.

Conclusion: Your input is actually ISO-8859-1. That's why it accidentally worked for you without the /u. Change that, and be eaxact with input charsets.

这篇关于preg_match 和(非英语)拉丁字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Create preg_match for password validation allowing (!@#$%)(为密码验证创建 preg_match 允许 (!@#$%))
Extracting City and Zipcode from String in PHP(用PHP从字符串中提取城市和邮政编码)
Seprate movie name and year from moviefile name(将电影名称和年份与电影名称分开)
Get part of string using php(使用php获取部分字符串)
Replace and retrieve placeholder value(替换和检索占位符值)
preg_replace change link from href(PRIG_REPLACE来自HREF的更改链接)