PHP正则表达式通配符url与url匹配

时间:2014-11-12 17:44:38

标签: php regex

我正在尝试将带有通配符的网址与实际网址进行匹配。

例如:

http://*.example.com/product/*/shop/

需要匹配

http://a.example.com/product/table/shop/
http://b.example.com/product/shoe

到目前为止,我已尝试过以下正则表达式,并且在所有情况下都无效:

/([a-zA-Z0-9\-_*]+\.)?([a-zA-Z0-9\-_]+\.[a-zA-Z\.]{1,}[^\" ]+)/i

/([https?:\/\/])(\*.)?([^\" ]+)(\*.)?/i

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

    $re = "/(https?:\\/\\/)([a-zA-Z0-9]+)\\.(example.com\\/product\\/)([a-zA-Z0-9]+)(\\/shop)\\/?/";
    $str = "http://a.example.com/product/table/shop/\nhttp://b.example.com/product/shoe/";
    $subst = "";

    $result = preg_replace($re, $subst, $str);

Live Demo here