my aim
表示"作者名输入",以确保只输入空格和utf-8字母。我的网站语言是土耳其语,土耳其语字母有非英语字符。
我奇怪的问题
这个正则表达式适用于rubular.com
如果输入字符串:"Selim Çınar"结果:匹配
如果输入字符串:"Selim Çınar 12"结果:不匹配
regex: /^['p{L} ]+$/u
然后我在我的网站上创建了trial.php并运行下面的代码
1
echo '<br /><br /><br />';
$str ='Selim Cinar';
if (!preg_match("/^['p{L} ]+$/u", $str))
{echo 'no, not only utf-8 letters and spaces';}
else {$str.' yes utf-8 letters and spaces';}
echo '<br /><br /><br />';
的结果:在源页只有<br />
标记的空页
echo '<br /><br /><br />';
$str ='Selim Çınar'; //includes Tr characters
if (!preg_match("/^['p{L} ]+$/u", $str))
{echo 'no, not only utf-8 letters and spaces';}
else {$str.' yes utf-8 letters and spaces';}
echo '<br /><br /><br />';
结果:源页只有<br />
标记的空页
3
代码来源:http://php.net/manual/en/function.ctype-alpha.php
$str ='Selim Çınar'; //includes Tr characters
$str =trim($str);
$str = str_replace(' ', '', $str);
setLocale(LC_CTYPE, 'TR_tr.UTF-8');
if (ctype_alpha($str)) {echo 'yes utf-8 letters';}
else {echo 'no, not only utf-8 letters';}
结果: no, not only utf-8字母
4代码来源:http://php.net/manual/en/function.ctype-alpha.php
$str ='Selim Cinar';
$str =trim($str);
$str = str_replace(' ', '', $str);
setLocale(LC_CTYPE, 'TR_tr.UTF-8');
if (ctype_alpha($str)) {echo 'yes utf-8 letters';}
else {echo 'no, not only utf-8 letters';}
结果: yes utf-8字母
我phpinfoPHP Version 5.4.10
Apache 2.0 Handler
Apache API版本:20051115
PCRE (Perl兼容正则表达式)支持启用
PCRE库版本8.20 2011-10-21
trial.php是纯php。没有HTML头声明。
<标题>我的问题- 为什么我得到空页的情况1和情况2 {!
- 为什么案例3不理解"SelimÇınar"utf - 8 ?是我的代码吗?假的(
setLocale
部分可能)?
问题1由MikeM解决。
**问题2仍然作为一个问题存在
标题>标题>不要使用setlocale
或utf8_decode
,您的问题非常简单,因为您的php源文件没有保存在UTF-8中。这取决于你的文本编辑器。
当你正确保存你的文件时,这将会工作:
$str = 'Selim Çınar'; //Since this is a string literal, its encoding is determined by
//how this source file was saved
if (preg_match("/^['p{L} ]+$/u", $str)) {
echo 'yes only utf-8 letters or spaces';
} else {
echo 'no, not only utf-8 letters or spaces';
}
对于情况1和2,您只得到一个空页,因为regex成功匹配$str
,因此执行else
分支,但是没有echo
,因此没有打印任何内容。
setLocale
对我来说看起来不错,但它的行为是系统依赖的。