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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • george

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

    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 !!!!!!!!!!!!!!! !!!!
  • Jeffrey Silverman

    #2
    Re: Remove from $name (folder)/(folder)/[x]/ &lt;-- if exists to just get [x].

    On Tue, 05 Aug 2003 20:46:37 -0700, george wrote:
    [color=blue]
    > 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 !!!!!!!!!!!!!!! !!!![/color]

    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/

    Comment

    • matty

      #3
      Re: Remove from $name (folder)/(folder)/[x]/ &lt;-- if exists to just get [x].

      Jeffrey Silverman wrote:

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

      REQUEST_URI tends to be a good choice, if you're using URL rewriting...

      Comment

      • Jeffrey Silverman

        #4
        Re: Remove from $name (folder)/(folder)/[x]/ &lt;-- if exists to just get [x].

        On Thu, 07 Aug 2003 16:09:24 -0700, george wrote:
        [color=blue]
        > 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*[/color]

        do array_pop() twice, then:
        <?
        $path = preg_replace("/^\/|\/$/", "", $_SERVER['PATH_TRANSLATE D']);
        $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/

        Comment

        • matty

          #5
          Re: Remove from $name (folder)/(folder)/[x]/ &lt;-- if exists to just get [x].

          Jeffrey Silverman wrote:
          [color=blue]
          >
          > 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!
          >
          >[/color]
          Hence the regex solution...

          Comment

          • george

            #6
            Re: Remove from $name (folder)/(folder)/[x]/ &lt;-- if exists to just get [x].

            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_TRANSLATE D']);
            $path_array = split("/", $path);
            array_pop($path _array);
            $page_name = array_pop($path _array);
            echo $page_name;
            ?>

            Thanksalot

            Comment

            Working...