Forum Moderators: coopster

Message Too Old, No Replies

Return URL path - including parameters?

         

steww

12:10 am on May 5, 2010 (gmt 0)

10+ Year Member



Hi there, hope all doing well.

Im trying to split a url and return the just the path including parameters (not the domain name) - and store it as a variable.

Thus for example my domain is: www.mydomain.com/pathone/path2/product.cfm?p=176

and I'd like to return:

$path = /pathone/path2/product.cfm?p=176

----------------------------------------------

Currently I'm using:

$Product_URL = www.mydomain.com/pathone/path2/product.cfm?p=176

$path = parse_url($Product_URL, PHP_URL_PATH);

----------------------------------------------

but if I echo $path I just get: /pathone/path2/product.cfm


Anyone got any ideas?

Many Thanks

rocknbil

2:01 am on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what is parse_url? It's nixing the query string for sure. Anyway here's one way.

<?php
header("content-type:text/html");
$string = 'www.mydomain.com/pathone/path2/product.cfm?p=176';
$paths = explode('/',$string);
$dump_this = array_shift($paths);
$come_together_lennon_said = '/' . join('/',$paths);
echo "$string<br><br>$come_together_lennon_said";
?>

Another, if it's anything after the first slash,

<?php
header("content-type:text/html");
$string = 'www.mydomain.com/pathone/path2/product.cfm?p=176';
$string = preg_replace('/[^\/]+?(\/.*)/',"$1",$string);
echo $string;
?>

jatar_k

1:39 pm on May 5, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you're getting exactly what you asked for
[php.net...]

you need to add params

$path = parse_url($Product_URL);
$mypath = $path['path'] . $path['query'];
echo $mypath;

Readie

2:41 pm on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yet another solution:
function get_full_query($input) {
if(preg_match('/^(?:[^\/]+)(\/[^\/]+)+\/?$/', $input, $out)) {
return $out[1];
}
}
And then you just pass the URL to get_full_query(), and for the following:

http://www.example.com/stuff/some_file.php?hello=world

It should return

/stuff/some_file.php?hello=world

rocknbil

6:13 pm on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what is parse_url?


DOH! Never knew there was a function for it, it's so basic I never looked . . . as you were! :-)

steww

8:18 pm on May 5, 2010 (gmt 0)

10+ Year Member



Hi all many thanks for the above soloutions - big help!

For what I need I think this one will fit:

$path = parse_url($Product_URL);
$mypath = $path['path'] . $path['query'];
echo $mypath;


If any issues I'll post back -

Many Thanks