0

我有 2 个这样的链接:
http ://www.site.com/report/4fbb14
http://www.site.com/4fbb14

所以$_SERVER['REQUEST_URI']对他们来说就像:

/报告/4fbb14
/4fbb14

我只需要得到4fbb14

我用过

$r_url = str_replace("/","",$_SERVER['REQUEST_URI']);

但是我需要它对两个链接都有效,这样的事情可以接受吗?

$r_url = str_replace("(/ or report)","",$_SERVER['REQUEST_URI']);
4

4 回答 4

4

更简单的是

$r_url = end(explode('/', $_SERVER['REQUEST_URI']));

这给了你最后一个正斜杠之后的任何东西

于 2013-03-24T00:52:45.240 回答
2

All the previous answers will work, except for php NoOb's, but if all the links will have the same format you can just do this:

$r_url = $_SERVER['REQUEST_URI'];
$r_url = str_replace("report","",$r_url);
$r_url = str_replace("/","",$r_url);
echo $r_url;
于 2013-03-24T01:03:23.773 回答
2

使用正则表达式

寻找字符串末尾的值,就在/.

if ( preg_match("/\/(\d+)$/", "http://www.site.com/report/4fbb14", $result) )
{
  $value = $result[1];
}

使用parse_url和简单explode

$values = parse_url("http://www.site.com/report/4fbb14");
$parts_of_the_url = explode("/", $values['path']);
$result = end($parts_of_the_url);
于 2013-03-24T00:55:36.013 回答
1

如果你想得到4fbb14

<?php 

$ex = explode('/', 'http://www.site.com/4fbb14');
echo $ex['3'];
于 2013-03-24T00:57:32.563 回答