Help with string replace...

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

    Help with string replace...

    Hello everyone!
    I've got the problem with string replace,
    i've got a string like:
    /adsf/sdfd34/sdf/435ff/sdfdf
    and i need to delete the last characters before "/", so it would
    become:
    /adsf/sdfd34/sdf/435ff/
    I am trying to make a file manager of a sort, and need to make a link
    "UP", which would take the user up one level in a directory...
    I tried to use str_replace, seems its not the one,
    thn i tried to use preg_replace, seems it is the one i need, but i'm
    nowhere
    near there...
    Thats my last effort:
    $output = preg_replace("/(\/*$)/","/", $input);
    Could someone help me please?
    Thank you very much!
  • Guillaume Brocker

    #2
    Re: Help with string replace...

    Tihon wrote:
    [color=blue]
    > Hello everyone!
    > I've got the problem with string replace,
    > i've got a string like:
    > /adsf/sdfd34/sdf/435ff/sdfdf
    > and i need to delete the last characters before "/", so it would
    > become:
    > /adsf/sdfd34/sdf/435ff/
    > I am trying to make a file manager of a sort, and need to make a link
    > "UP", which would take the user up one level in a directory...
    > I tried to use str_replace, seems its not the one,
    > thn i tried to use preg_replace, seems it is the one i need, but i'm
    > nowhere
    > near there...
    > Thats my last effort:
    > $output = preg_replace("/(\/*$)/","/", $input);
    > Could someone help me please?
    > Thank you very much![/color]

    There is a nice function in the PHP function, section Filesystem, called
    dirname. Look at <http://www.php.net/manual/en/function.dirnam e.php>

    Regards.

    Guillaume

    Comment

    • Guillaume Brocker

      #3
      Re: Help with string replace...

      Tihon wrote:
      [color=blue]
      > Hello everyone!
      > I've got the problem with string replace,
      > i've got a string like:
      > /adsf/sdfd34/sdf/435ff/sdfdf
      > and i need to delete the last characters before "/", so it would
      > become:
      > /adsf/sdfd34/sdf/435ff/
      > I am trying to make a file manager of a sort, and need to make a link
      > "UP", which would take the user up one level in a directory...
      > I tried to use str_replace, seems its not the one,
      > thn i tried to use preg_replace, seems it is the one i need, but i'm
      > nowhere
      > near there...
      > Thats my last effort:
      > $output = preg_replace("/(\/*$)/","/", $input);
      > Could someone help me please?
      > Thank you very much![/color]

      There is a nice function called dirname in the PHP toolbox, under the
      section Filsystem. It could repond to your needs.

      See <http://www.php.net/manual/en/function.dirnam e.php>

      Regards.

      Guillaume

      Comment

      • Martin Lucas-Smith

        #4
        Re: Help with string replace...



        [color=blue][color=green]
        > > Hello everyone!
        > > I've got the problem with string replace,
        > > i've got a string like:
        > > /adsf/sdfd34/sdf/435ff/sdfdf
        > > and i need to delete the last characters before "/", so it would
        > > become:
        > > /adsf/sdfd34/sdf/435ff/
        > > I am trying to make a file manager of a sort, and need to make a link
        > > "UP", which would take the user up one level in a directory...[/color][/color]

        I'm unclear why a simple "<a href="../"> won't work, instead of trying to
        compute what the reference should be.


        Martin

        Comment

        • Pedro Graca

          #5
          Re: Help with string replace...

          Tihon wrote:[color=blue]
          > i've got a string like:
          > /adsf/sdfd34/sdf/435ff/sdfdf
          > and i need to delete the last characters before "/", so it would
          > become:
          > /adsf/sdfd34/sdf/435ff/[/color]
          (snip)

          Why not combine substr() and strrpos()?

          Find the position of the last occurrence of a substring in a string


          $output = substr($input, 0, strrpos($input, '/'));



          when $input is '/one/two'

          strrpos($input, '/') will give the position of the last '/' in it (4 in
          this case); substr() will return '/one'

          Verify whether you need the last character to be a '/' and modify that
          line accordingly.

          Don't forget to check for inputs without '/'s.
          --
          --= my mail box only accepts =--
          --= Content-Type: text/plain =--
          --= Size below 10001 bytes =--

          Comment

          • Justin Koivisto

            #6
            Re: Help with string replace...

            Martin Lucas-Smith wrote:
            [color=blue][color=green][color=darkred]
            >>>Hello everyone!
            >>>I've got the problem with string replace,
            >>>i've got a string like:
            >>>/adsf/sdfd34/sdf/435ff/sdfdf
            >>>and i need to delete the last characters before "/", so it would
            >>>become:
            >>>/adsf/sdfd34/sdf/435ff/
            >>>I am trying to make a file manager of a sort, and need to make a link
            >>>"UP", which would take the user up one level in a directory...[/color][/color]
            >
            >
            > I'm unclear why a simple "<a href="../"> won't work, instead of trying to
            > compute what the reference should be.[/color]

            using mod_rewrite may be one good reason...

            --
            Justin Koivisto - spam@koivi.com
            PHP POSTERS: Please use comp.lang.php for PHP related questions,
            alt.php* groups are not recommended.
            Official Google SERPs SEO Competition: http://www.koivi.com/serps.php

            Comment

            • Tihon

              #7
              Re: Help with string replace...

              > Tihon wrote:[color=blue][color=green]
              > > i've got a string like:
              > > /adsf/sdfd34/sdf/435ff/sdfdf
              > > and i need to delete the last characters before "/", so it would
              > > become:
              > > /adsf/sdfd34/sdf/435ff/[/color]
              > (snip)
              >
              > Why not combine substr() and strrpos()?
              > http://www.php.net/substr
              > http://www.php.net/strrpos
              >
              > $output = substr($input, 0, strrpos($input, '/'));
              >
              >
              >
              > when $input is '/one/two'
              >
              > strrpos($input, '/') will give the position of the last '/' in it (4 in
              > this case); substr() will return '/one'
              >
              > Verify whether you need the last character to be a '/' and modify that
              > line accordingly.
              >
              > Don't forget to check for inputs without '/'s.[/color]

              Thanks all of you so very much!
              Dirname does exactly what i need (never heard of it before, i
              definately must get a book! :()
              And combining substr and strrpos is a great way too!
              Thanks a lot!

              Comment

              • Tihon

                #8
                Re: Help with string replace...

                Pedro Graca <hexkid@hotpop. com> wrote in message news:<bvtsh8$10 42bj$1@ID-203069.news.uni-berlin.de>...[color=blue]
                > Tihon wrote:[color=green]
                > > i've got a string like:
                > > /adsf/sdfd34/sdf/435ff/sdfdf
                > > and i need to delete the last characters before "/", so it would
                > > become:
                > > /adsf/sdfd34/sdf/435ff/[/color]
                > (snip)
                >
                > Why not combine substr() and strrpos()?
                > http://www.php.net/substr
                > http://www.php.net/strrpos
                >
                > $output = substr($input, 0, strrpos($input, '/'));
                >
                >
                >
                > when $input is '/one/two'
                >
                > strrpos($input, '/') will give the position of the last '/' in it (4 in
                > this case); substr() will return '/one'
                >
                > Verify whether you need the last character to be a '/' and modify that
                > line accordingly.
                >
                > Don't forget to check for inputs without '/'s.[/color]

                My previous post does not seem to appear,
                Thanks to all of you, thats exactly what i needed!
                Thank you!

                Comment

                Working...