开发者

Retrieve Link ID

开发者 https://www.devze.com 2023-03-09 07:07 出处:网络
Let say we have link as following for link as ****.com/index.php?id=4 the <?=$id?> is 4 for link开发者_Go百科 as ****.com/4

Let say we have link as following

for link as ****.com/index.php?id=4

the <?=$id?> is 4

for link开发者_Go百科 as ****.com/4

the <?=$id?> is 4

for link as ****.com/4-keyowrd-keyword

$realid = array_shift(explode("-", $id));

so <?=$realid?> is 4

Now my question is for link as ****.com/4/keyword-keyword

How then get the id as 4 is there any way to do it ?

Thanks


If you actually have a complete URL with the scheme included (i.e.: http://something.com and not just something.com), you can do:

// Search in the query string
$url = 'http://something.php/index.php?id=4';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $queryArr);
$id = $queryArr['id'];
echo $id; // 4

// Search in the path
$url = 'http://something.com/4/keyword-keyword';
$path = parse_url($url, PHP_URL_PATH);
$id = basename(dirname($path));
echo $id; // 4

// Search in the path (undefined length)
$url = 'http://something.com/4/keyword-keyword/foo/bar';
$path = parse_url($url, PHP_URL_PATH);
list(,$id) = explode('/', $path);
echo $id; // 4

If you don't the scheme there, you can just prepend it, like:

$url = 'http://'.$url;


Use $_SERVER['PHP_SELF'] and explode the \to get your variable


$realid = array_shift(explode("/", $_SERVER['REQUEST_URI']));
0

精彩评论

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