PHP: How to convert a regular expression to a match example?

I have a regex to match URIs. For instance,

preg_match("/^my\/uri\//i", "my/uri/whatever");

What I use for routing, for example, " http://www.mywebsite.com/my/uri/page.html " will match the above (with remote protocol / host).

Is there a way to evaluate the regex in the most common URI that will match? For instance,

"my / uri /"

+2
source share
2 answers

I did not understand what you really want.

This code may be what you need:

$general_uri = 'my/uri/';
$regex = '/^' . preg_quote($general_uri) . '/i';

If you want to change the code above:

$regex = '/^my\/uri\//i';
$general_uri = str_replace('\\', '', preg_replace('/^\/\^(.*)\/i?$/', '$1', $regex));

However, the above code will not work on complex regular expressions.

+1
source

, , , - .

, '/', , - Magento Zend Framework , .

, PHP.

0

Source: https://habr.com/ru/post/1731759/


All Articles