【问题标题】:preg_match special character like Turkish and otherpreg_match 特殊字符,如土耳其语和其他
【发布时间】:2021-04-09 18:54:07
【问题描述】:

我正在使用以下代码来获取 url 匹配,但是当我使用一些特殊字符(如土耳其语)例如 ülkeaşk 时,我无法收到任何请求。但是如果我使用 ulke , ask 那么我可以得到一个结果。

谁能告诉我这里有什么问题?

$request_uri = 'https://www.weburl.com/hashtag/ulke'; // working fine
$request_uri = 'https://www.weburl.com/hashtag/ülke'; // no result

if (preg_match('~/(hashtag)/([[\w.-]+)~', $request_uri, $match)) {
   $pageFor = $match[2];
}

【问题讨论】:

    标签: php preg-match


    【解决方案1】:

    您可能需要使用u 修饰符告诉preg_match 将字符串视为UTF-8:

    https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

    $request_uri = 'https://www.weburl.com/hashtag/ülke';
    
    if (preg_match('~/(hashtag)/([[\w.-]+)~u', $request_uri, $match)) {
       $pageFor = $match[2];
    }
    
    echo $pageFor; //ülke
    

    【讨论】:

    猜你喜欢
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    • 2014-07-25
    相关资源
    最近更新 更多