N
N
Nurasyl Mels2020-10-31 13:55:45
PHP
Nurasyl Mels, 2020-10-31 13:55:45

The regular expression does not see the "-" character. How to decide?

There are url patterns, but regular expressions don't see the "-" sign.

Here is the code for the regular expression:

$to_match = preg_replace('/{([a-z]+):([^\}]+)}/', '(?P<\1>\2)', $to_match);
$to_match = '#^'.$to_match.'$#';


Problem: Given a template, such as item/{name:\w+}. If you go to the address item/item_name, then everything is fine. But if you go to item/item-name, it no longer finds matches.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Gnevyshev, 2020-10-31
@iResource

Before sending the template, try to escape special characters in it, incl. hyphen. So instead, item/item-namepass it like this:item\/item\-name

M
Max, 2020-10-31
@7workers

https://www.php.net/manual/en/function.preg-quote.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question