URLs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J Huntley Palmer

    #1

    URLs

    How may I capture the last /.../ in a url?

    eg.. www.foo.com/foo/bar/baz/index.php?param=1

    I want to capture 'baz'.

    Thanks
  • lorento

    #2
    Re: URLs

    Use parse_url() function : http://www.php.net/function.parse-url

    $url = www.foo.com/foo/bar/baz/index.php?param=1
    $url_arr = explode ("//", parse_url($url) )

    $i = count($url_arr) - 1;
    $last_dir = $url_arr[$i];

    Not tested yet, but i think its work.

    --



    J Huntley Palmer wrote:[color=blue]
    > How may I capture the last /.../ in a url?
    >
    > eg.. www.foo.com/foo/bar/baz/index.php?param=1
    >
    > I want to capture 'baz'.
    >
    > Thanks[/color]

    Comment

    • Joe Estock

      #3
      Re: URLs

      J Huntley Palmer wrote:[color=blue]
      > How may I capture the last /.../ in a url?
      >
      > eg.. www.foo.com/foo/bar/baz/index.php?param=1
      >
      > I want to capture 'baz'.
      >
      > Thanks[/color]

      Start from the end of the string and work backwards until you hit the
      second /. Untested code follows:

      $flag = false;
      for ($i = strlen($url); $i > 0; $i--)
      {
      if ($url{$i} == '/')
      {
      if ($flag == true)
      {
      /* we have our match */
      $url = substr($url, $i);
      break;
      }

      /* got the first / */
      $flag = true;
      }
      }

      Comment

      • Joe Estock

        #4
        Re: URLs

        Joe Estock wrote:[color=blue]
        > J Huntley Palmer wrote:
        >[color=green]
        >> How may I capture the last /.../ in a url?
        >>
        >> eg.. www.foo.com/foo/bar/baz/index.php?param=1
        >>
        >> I want to capture 'baz'.
        >>
        >> Thanks[/color]
        >
        >
        > Start from the end of the string and work backwards until you hit the
        > second /. Untested code follows:
        >
        > $flag = false;
        > for ($i = strlen($url); $i > 0; $i--)
        > {
        > if ($url{$i} == '/')
        > {
        > if ($flag == true)
        > {
        > /* we have our match */
        > $url = substr($url, $i);
        > break;
        > }
        >
        > /* got the first / */
        > $flag = true;
        > }
        > }[/color]

        Whoops, misread your intent. Corrections follow (again, untested).

        $flag = false;
        $j = 0;
        for ($i = strlen($url); $i > 0; $i--)
        {
        if ($url{$i} == '/')
        {
        if ($flag == true)
        {
        /* we have our match */
        $url = substr($url, $i, ($j - $i));
        break;
        }

        /* got the first / */
        $flag = true;
        $j = $i;
        }
        }

        Comment

        • AlexVN

          #5
          Re: URLs


          J Huntley Palmer wrote:[color=blue]
          > How may I capture the last /.../ in a url?
          >
          > eg.. www.foo.com/foo/bar/baz/index.php?param=1
          >
          > I want to capture 'baz'.
          >
          > Thanks[/color]

          Just my $0.02:

          $t = parse_url('www. foo.com/foo/bar/baz/index.php?param =1');
          $t = pathinfo($t['path']);
          $t = explode('/', $t['dirname']);
          $t = array_pop($t);
          var_dump('baz' == $t);

          Sincerely,
          Alexander
          http://www.alexatnet.com/

          Comment

          Working...