开发者

PHP: How do I convert a regular expression to an example match?

I have a regular expression for matching URIs. For example,

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

Which I use for routing, for exmample "http://www.mywebsite.com/my/uri/page.html" will match the above (with the protocol/host removed of course).

Is there any way to evaluate the regular expression into the most general URI tha开发者_Python百科t will match? For example,

"my/uri/"


I didn't understand what you actually want.

This code might be what you need:

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

If you want reverse of the above code:

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

However above code will not work on complicated regexes.


Maybe if you can tell the original problem that leads you into this dead end someone can give a twist on the situation.

I've made myself a routing algoritm and I just use and explode on '/' and it works very nicely, something like Magento or Zend Framework does it, registering path controllers or routers and linking them.

Maybe your original problem can be solved without need to write a regular expression engine with PHP.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜