localization - rest api endpoint - accept diacritic characters

I am trying to register a custom endpoint, which will accept diacritic (accent) characters, so that I may search it in f

I am trying to register a custom endpoint, which will accept diacritic (accent) characters, so that I may search it in french.

This is what I have tried:

register_rest_route('events/v1', 'organizations/(?P<search>[a-zA-ZÀ-ž][\wÀ-ÿ]+)' array(
    'methods' => WP_REST_Server::READABLE,
    'callback' => 'get_event_organizations',
    'args' => [
        'search',
    ],
));)

The regex should work :

But the endpoint returns a rest_no_route / 404


EDIT

Thanks @Tim I was overcomplicating it!

register_rest_route('events/v1', 'organizations/(?P<search>[\w].+)' array(
    'methods' => WP_REST_Server::READABLE,
    'callback' => 'get_event_organizations',
    'args' => [
        'search',
    ],
));)

I am trying to register a custom endpoint, which will accept diacritic (accent) characters, so that I may search it in french.

This is what I have tried:

register_rest_route('events/v1', 'organizations/(?P<search>[a-zA-ZÀ-ž][\wÀ-ÿ]+)' array(
    'methods' => WP_REST_Server::READABLE,
    'callback' => 'get_event_organizations',
    'args' => [
        'search',
    ],
));)

The regex should work : https://regex101/r/aM6tV1/41

But the endpoint returns a rest_no_route / 404


EDIT

Thanks @Tim I was overcomplicating it!

register_rest_route('events/v1', 'organizations/(?P<search>[\w].+)' array(
    'methods' => WP_REST_Server::READABLE,
    'callback' => 'get_event_organizations',
    'args' => [
        'search',
    ],
));)
Share Improve this question edited May 30, 2019 at 15:50 MediaFormat asked May 29, 2019 at 18:00 MediaFormatMediaFormat 2831 silver badge11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Looks to me like WordPress doesn't enable UTF-8 mode when it runs the regex.

https://github/WordPress/WordPress/blob/5.2.1/wp-includes/rest-api/class-wp-rest-server.php#L837

In PHP, matching multibyte characters requires the u flag. (The code above just adds the case-insensitive flag). So although some strings will still match (like "Département"), they're not matching as whole Unicode characters. For example the following pattern matches when it appears is shouldn't.

preg_match('@^[a-zA-ZÀ-ž][\\wÀ-ÿ]+$@i','£');
// returns 1

The "£" is matched, because it's made of two bytes (C2 A3). With UTF-8 mode enabled it doesn't match at the start of the string, because as a character (00A3) it's greater than "z" (007A) but less than "À" (00C0) in the first character range.

preg_match('@^[a-zA-ZÀ-ž]@iu','£');
// returns 0

So without seeing a specific input string that you're failing to match, this might explain why it's not working. Probably you don't need to be specifying such a specific pattern and can perhaps match any byte with .+ following a safely matched prefix.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745450389a4628239.html

相关推荐

  • localization - rest api endpoint - accept diacritic characters

    I am trying to register a custom endpoint, which will accept diacritic (accent) characters, so that I may search it in f

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信