开发者

Use preg_match to grab value from URL

I have a url something like:

www.yourname.mysite.com/templates/diversity/index.php

I need to grab that string, "diversity", and match it against a db value. I've never used anything like preg_match in php, how can I 开发者_如何学Gograb that string?


Instead of a regular expression, you can use parse_url() for that:

$url = 'www.yourname.mysite.com/templates/diversity/index.php';

$parts = parse_url($url);
$paths = explode('/', $parts['path']);
// "diversity" is in $paths[1]

For completeness' sake, here's the regular expression:

preg_match('=^[^/]+/[^/]+/([^/]+)/=', $url, $matches);
// "diversity" is in $matches[1]


    $ex = explode("/",$_SERVER["PHP_SELF"]);
    echo $ex[2];


You can just explode that string and get the third value of the resulting array:

$parts = explode("/", "www.yourname.mysite.com/templates/diversity/index.php");
echo $parts[2]; // diversity
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜