Search code examples
phpregexwordpressarabicwordpress-rest-api

Unable to read Arabic character from Wordpress REST API


I'm trying to implement search REST API for Wordpress. Everything works fine in English, however, when I enter some Arabic in the URL, it would tell me rest_no_route.

This it my working version for English

        register_rest_route(
            'search', 
            '/keywords/(?P<value>[\w+].+)', 
            $args
        );

After done some research, it was suggest that I should add [ء-ي] to my regex. However, I'm not quite sure where to add in the regex as Arabic reads from right to left.

I have tried (?P<value>[ء-ي][\w+].+) or [ء-ي](?P<value>[\w+].+) it still won't work.

Any suggestions?


Solution

  • The \w shorthand in PHP, be default, does not match Unicode letters and digits. You would need to use (*UCP) (probably with (*UTF)) PCRE verb to make it match all Unicode letters/digits. See an example.

    In your case, you may simply remove \w+ and keep .+ to match any 1+ chars other than line break chars:

    /keywords/(?P<value>.+)