开发者

PHP Get From URL

I was wondering how I could remove a certain part of a url using PHP.

This is the exact url in questions:

h开发者_运维问答ttp://www.mysite.com/link/go/370768/

I'm looking to remove the number ID into a variable.

Any help would be great, Thanks!


There are many (many!) ways of extracting a number from a string. Here's an example which assumes the URL starts with the format like http://www.mysite.com/link/go/<ID> and extracts the ID.

$url = 'http://www.mysite.com/link/go/370768/';
sscanf($url, 'http://www.mysite.com/link/go/%d', $id);
var_dump($id); // int(370768)


Use explode()

print_r(explode("/", $url));


You could use mod_rewrite inside of your .htaccess to internally rewrite this URL to something more friendly for PHP (convert /link/go/370768/ into ?link=370768, for example).


I suspect that you are using some kind of framework. There are two ways to check the $_GET variables:

print_r($_GET);

or check the manual of the manual of the framework and see how the GET/POST is passed internally, for example in CakePHP you have all parameters save internally in your controller you can access them like that:

$this->params['product_id'] or $this->params['pass']

There is another solution which is not very reliable and professional but might work:

$path = parse_url($url, PHP_URL_PATH);
$vars = explode('/', $path);
echo $vars[2];

$vars should contain array like this:

array (
  0 => link
  1 => go
  2 => 370768
)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜