0

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

This is the exact url in questions:

http://www.mysite.com/link/go/370768/

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

Any help would be great, Thanks!

4
  • I'm not sure but maybe you're looking for mod_rewrite to alter your URL to something nicer. Commented Jul 17, 2011 at 10:40
  • Is your number ID always in the same position and does it always have 6 digits followed by a slash? Commented Jul 17, 2011 at 10:41
  • Maybe you can use this php.net/parse-url Commented Jul 17, 2011 at 10:42
  • 1
    do you use some kind of Framework or is there .htaccess file in your app route that rewrites the URL, there should be one. Commented Jul 17, 2011 at 10:47

4 Answers 4

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

Use explode()

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

Comments

0

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).

Comments

0

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
)

2 Comments

you meaned get with URL in it?
$_GET is internall PHP array that contains all variables sent in the URL: php.net/manual/en/reserved.variables.get.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.