PHP正则表达式不';在我的网站上不工作,但在正则表达式编辑器上工作 我的目标
对于“作者姓名输入”,确保只输入空格和utf-8字母。我的网站lang是土耳其语,土耳其语字母表有非英语字符 我奇怪的问题 此正则表达式在rubular.com上运行PHP正则表达式不';在我的网站上不工作,但在正则表达式编辑器上工作 我的目标,php,regex,utf-8,preg-match,spaces,Php,Regex,Utf 8,Preg Match,Spaces,对于“作者姓名输入”,确保只输入空格和utf-8字母。我的网站lang是土耳其语,土耳其语字母表有非英语字符 我奇怪的问题 此正则表达式在rubular.com上运行 如果输入字符串:“Selimınar”结果:匹配 如果输入字符串:“Selimınar 12”结果:不匹配 正则表达式:/^[\p{L}]+$/u 然后我在我的网站上创建了trial.php并运行下面的代码 1. 以上代码的结果:不,不仅仅是utf-8字母 4. 代码来源: 上述代码的结果:是utf-8字母 我的phpinfo P
如果输入字符串:“Selimınar”结果:匹配
如果输入字符串:“Selimınar 12”结果:不匹配
正则表达式:
/^[\p{L}]+$/u
然后我在我的网站上创建了trial.php并运行下面的代码
1.
以上代码的结果:不,不仅仅是utf-8字母
4.
代码来源:
上述代码的结果:是utf-8字母
我的phpinfo
PHP版本5.4.10Apache 2.0处理程序
Apache API版本:20051115
已启用PCRE(与Perl兼容的正则表达式)支持
PCRE库版本8.20 2011-10-21
关于trial.php php是纯php。没有html头声明 我的问题
setLocale
part-maybe)**问题2仍然作为一个问题存在。对于案例1和案例2,您只得到一个空页,因为正则表达式成功地匹配了
$str
,因此执行了else
分支,但没有echo
,因此没有打印任何内容
我不知道你第二个问题的答案。
setLocale
看起来不错,但它的行为取决于系统。不要使用setLocale
或utf8\u 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';
}
谢谢你,迈克。我更新了我的问题。我的第一个问题已经被你解决了。我不知道你为什么这么肯定OP的源文件不是UTF-8。即使是UTF-8,OP第二个问题的代码也给出了“不,不仅仅是UTF-8字母”,他想知道这是为什么。@MikeM那么即使你的源文件不是UTF-8,它也不会为我打印任何东西。那是因为else中没有
echo
。@MikeM-hmm,等等,你是指2.
片段还是问题中的最后一个片段?@MikeM我明白了,我对此一无所知,因为首先使用ctype.*
函数是没有意义的。:)@Esailija好的,那么我理解如果我声明HTML lang是utf-8,那么我就不会有与我的问题2相对应的案例3和案例4。非常感谢。
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 />';
$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';}
$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';}
$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';
}