Search code examples
phppreg-match

preg_match special character like Turkish and other


I am using following code for getting url match but when I use some special character like Turkish for example ülke, aşk then I can not get any request. but if I use ulke , ask then the I can get a result.

Anyone can tell me what is the issue here ?

$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];
}

Solution

  • You probably need to use the u modifier to tell preg_match to treat strings as 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