Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP正则表达式不';在我的网站上不工作,但在正则表达式编辑器上工作 我的目标_Php_Regex_Utf 8_Preg Match_Spaces - Fatal编程技术网

PHP正则表达式不';在我的网站上不工作,但在正则表达式编辑器上工作 我的目标

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

对于“作者姓名输入”,确保只输入空格和utf-8字母。我的网站lang是土耳其语,土耳其语字母表有非英语字符

我奇怪的问题 此正则表达式在rubular.com上运行
如果输入字符串:“Selimınar”结果:匹配
如果输入字符串:“Selimınar 12”结果:不匹配
正则表达式:
/^[\p{L}]+$/u

然后我在我的网站上创建了trial.php并运行下面的代码

1. 以上代码的结果:不,不仅仅是utf-8字母

4. 代码来源: 上述代码的结果:是utf-8字母

我的phpinfo PHP版本5.4.10
Apache 2.0处理程序
Apache API版本:20051115
已启用PCRE(与Perl兼容的正则表达式)支持
PCRE库版本8.20 2011-10-21

关于trial.php php是纯php。没有html头声明

我的问题
  • 为什么我得到案例1和案例2的空白页面{!更新:由下面的MikeM解决}
  • 为什么案例3不将“Selimınar”理解为utf-8?这是我的密码 false(
    setLocale
    part-maybe)
  • 更新 问题1由MikeM解决。


    **问题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';
    }