By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Remove from $name (folder)/(folder)/[x]/ <-- if exists to just get [x].

P: n/a
Guys and girls, I'll be quite honest. I don't have the faintest idea
how to do this, while I can do other php without a problem. Once I
know how to go about it I'll be okay.
It's two things I think?
1. see if there's a trailing slash and delete it.
2. Remove the preceding path to /x, the folders and slashes, and
delete them.
and then I have x.

X by the way is the final folder in a website and this grabs it for
me.

<?
$name = $REQUEST_URI;
?>

Any help would be greatly appreciated.
Thanks !!!!!!!!!!!!!!!!!!!
Jul 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Tue, 05 Aug 2003 20:46:37 -0700, george wrote:
Guys and girls, I'll be quite honest. I don't have the faintest idea how
to do this, while I can do other php without a problem. Once I know how to
go about it I'll be okay. It's two things I think?
1. see if there's a trailing slash and delete it. 2. Remove the preceding
path to /x, the folders and slashes, and delete them.
and then I have x.

X by the way is the final folder in a website and this grabs it for me.

<?
$name = $REQUEST_URI;
?>

Any help would be greatly appreciated. Thanks !!!!!!!!!!!!!!!!!!!


Warning! Untested code follows...
<?php

// Make sure path ends in no slash (optional step you are *certain* that
// the URL will or will not end in a slash):
$request_uri = preg_replace("/\/*$/", "", $REQUEST_URI);

// Split the path into an array:
$path_array = split("/", $REQUEST_URI);

// pop off the end:
$x = array_pop($path_array);

echo "The URL ends in $x\n";
?>

later...
--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Johns Hopkins University | Baltimore, MD
Website | http://www.wse.jhu.edu/newtnotes/

Jul 16 '05 #2

P: n/a
Jeffrey Silverman wrote:

<big snip>
On second thought, maybe i *should* have used $REQUEST_URI! Other than
that, I'm not sure what the trouble is...


REQUEST_URI tends to be a good choice, if you're using URL rewriting...
Jul 16 '05 #3

P: n/a
On Thu, 07 Aug 2003 16:09:24 -0700, george wrote:
Hi Jeffrey.
Hmm.
No, this again gives me the originating page, not the folder that it's in,
ie, test.php, rather than [x].
*grin*


do array_pop() twice, then:
<?
$path = preg_replace("/^\/|\/$/", "", $_SERVER['PATH_TRANSLATED']);
$path_array = split("/", $path);
array_pop($path_array);
$page_name = array_pop($path_array);
?>

My questions to you are:
* What does the REQUEST_URI look like?
i.e. is it a directory (ends in a slash) or a file? The original code I
gave works if the URI ends in the *directory* name, not a file name!
* Do you understand the logic of the above code? What is it that I am
doing?

I'll explain in English what each line does. I find that for complex tasks
it is often best to speak out loud the steps or write them down as
sentences -- English sentences, not code sentences. Then convert English
logic to code logic. So, here goes:

1) Get URI; *assume* URI is a directory.
2) See if URI ends in a slash, if so, we want to get rid of it.
3) make a list of all the elements in the URI where the element delimeter
is the slash (/) character.
4) get the last item in the list.

Number 4 is the part that really assumes that the URI is a *directory* and
not a file! If the URI ends in a file name, number 4 shuold read "Take the
second-to-last item in the list"

Good luck!
--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Johns Hopkins University | Baltimore, MD
Website | http://www.wse.jhu.edu/newtnotes/

Jul 16 '05 #4

P: n/a
Jeffrey Silverman wrote:

I'll explain in English what each line does. I find that for complex tasks
it is often best to speak out loud the steps or write them down as
sentences -- English sentences, not code sentences. Then convert English
logic to code logic. So, here goes:

1) Get URI; *assume* URI is a directory.
2) See if URI ends in a slash, if so, we want to get rid of it.
3) make a list of all the elements in the URI where the element delimeter
is the slash (/) character.
4) get the last item in the list.

Number 4 is the part that really assumes that the URI is a *directory* and
not a file! If the URI ends in a file name, number 4 shuold read "Take the
second-to-last item in the list"

Good luck!

Hence the regex solution...

Jul 16 '05 #5

P: n/a
I'm very embarassed, as well as being thankful.
On my 1st read of your reply I didn't see *new* php.

It works a treat, from any folder.

<?
$path = preg_replace("/^\/|\/$/", "", $_SERVER['PATH_TRANSLATED']);
$path_array = split("/", $path);
array_pop($path_array);
$page_name = array_pop($path_array);
echo $page_name;
?>

Thanksalot
Jul 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.