Forum Moderators: coopster

Message Too Old, No Replies

Stripping url details

         

dwighty

9:47 pm on Feb 12, 2007 (gmt 0)

10+ Year Member



Hi Guys,

Not sure how to do this but basically I want to be able to extract the unique pagename from a url when the page has been requested. the structure i have is as follows:

www.my-domain-name.com/unique => This is for the initial page/section
www.my-domain-name.com/data1/unique => Where data1 is the parent id and unique is the page within the parent section.

so in the first case, i want to be able to extract unique from the url which is fine, i would use the following:
$page_url_new = "/".addslashes($_GET['unique']);

however if i test this method on a domain such as www.my-domain.com/about_us/project_manager then the unique value returns about_us/project_manager instead of just project_manager.

Not sure i have made this clear or not!

ahmedtheking

10:36 pm on Feb 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, well, do this:

Say the return is: (1) /uniquepage and (2) /uniqueplace/thispage

So...

$a = @explode("/",$url);
if (is_array($a)) {
$uniquepage = $a[count($a)-1];
} else {
$uniquepage = $a;
}

cameraman

10:40 pm on Feb 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or
$uniquepage = strrchr($url,'/');

That would start with the slash, which you may not want, in that case:
$uniquepage = substr(strrchr($url,'/'),1);

Psychopsia

10:43 pm on Feb 12, 2007 (gmt 0)

10+ Year Member



Hi!

I use Rewrite Mod to do this, more info here on Apache forum [webmasterworld.com].

In .htaccess:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(about_us)/?$ index.php?parent=$1 [nc]
RewriteRule ^(about_us)/(project_manager)/?$ index.php?parent=$1&child=$2 [nc]

On index.php you can handle the vars using $_GET['parent'] and $_GET['child'].

[edited by: Psychopsia at 10:45 pm (utc) on Feb. 12, 2007]

dwighty

9:42 am on Feb 13, 2007 (gmt 0)

10+ Year Member



Thanks for you replies.

I am still not getting the results i am after though.

The examples I gave were only up to 2 levels, however there can be 5/6 levels. Maybe the way I am going about the set-up could be better.

Basically, the files are all content pages - so the .com/welcome would be say pageid=1 and .com/welcome/about would be pageid=2 but a child of pageid=1 and .com/welcome/about/me could be pageid=3 but a child of pageid2.

I thought that the best way would be to strip down the url and only get the last item but have not been able to do so with the examples above.

maybe i am missing a trick here.

jatar_k

1:40 pm on Feb 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try echoing some data to the browser to see what you are working with

you could try these to see what is going on, also look through the $_SERVER [php.net] vars to see what you can use

$myuri = $_SERVER['REQUEST_URI'];

echo '<p>raw URI: ',$myuri;

$uriparts = parse_url [php.net]($myuri);

echo '<p><pre>parse_url:<br>';
print_r($uriparts);
echo '</pre>';

$uriparts2 = explode [php.net]('/',$myuri);

echo '<p><pre>explode<br>';
echo '</pre>';

this will mostly show what data you are getting and give you a better idea what you need to use and then what to do next

dwighty

3:39 pm on Feb 13, 2007 (gmt 0)

10+ Year Member



Hi jatar_k,

Thanks for your response. I had been previously trying to echo results but your notes have bought me 1 step closer to what I am looking for.

$myuri = $_SERVER['REQUEST_URI'];

$uriparts2 = explode('/',$myuri);

My next question is how would i get the last array line, so my current array is:
[0] =>
[1] => dev_site
[2] => meet-the-team
[3] => test
I would need to get unique = [3]. However I would not know how many lines there are as this is created dynamically.

Thanks.

mcibor

4:05 pm on Feb 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a function called end [php.net]

So just
$myuri = $_SERVER['REQUEST_URI'];

$uriparts2 = explode('/',$myuri);
echo end($uriparts2);

Regards
Michal

jatar_k

4:09 pm on Feb 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



end would work, which probably does the same as

$theone = $uriparts2[count($uriparts2) - 1]
echo $theone;

that's from the old days I guess ;)

mcibor

4:25 pm on Feb 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Old days sounds a bit archaic... :D

I just found about end few weeks ago, and I must say I like it!

I would also like strpos to find the latest appearance (without looping), but that would be just nice, not necessary.

dwighty

4:36 pm on Feb 13, 2007 (gmt 0)

10+ Year Member



Thank you very much, its just what i needed.

I think I will stick with the end() function to keep the text simply for me to remember.

Thanks again.