Professional Web Applications Themes

Help with string replace... - PHP Development

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? ...

  1. #1

    Default 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!
    Tihon Guest

  2. #2

    Default Re: Help with string replace...

    Tihon wrote:
     

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

    Regards.

    Guillaume
    Guillaume Guest

  3. #3

    Default Re: Help with string replace...

    Tihon wrote:
     

    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.dirname.php>

    Regards.

    Guillaume
    Guillaume Guest

  4. #4

    Default Re: Help with string replace...



     [/ref]

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


    Martin

    Martin Guest

  5. #5

    Default Re: Help with string replace...

    Tihon wrote: 
    (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.
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--
    Pedro Guest

  6. #6

    Default Re: Help with string replace...

    Martin Lucas-Smith wrote:
     [/ref]
    >
    >
    > I'm unclear why a simple "<a href="../"> won't work, instead of trying to
    > compute what the reference should be.[/ref]

    using mod_rewrite may be one good reason...

    --
    Justin Koivisto - 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
    Justin Guest

  7. #7

    Default Re: Help with string replace...

    > Tihon wrote: 
    > (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.[/ref]

    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!
    Tihon Guest

  8. #8

    Default Re: Help with string replace...

    Pedro Graca <com> wrote in message news:<bvtsh8$1042bj$news.uni-berlin.de>... 
    > (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.[/ref]

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

Similar Threads

  1. replace() string
    By dweeres in forum Coldfusion - Getting Started
    Replies: 3
    Last Post: May 1st, 04:26 AM
  2. Replace String
    By Michael42 in forum Macromedia ColdFusion
    Replies: 1
    Last Post: February 22nd, 06:58 PM
  3. replace spaces with 0's in a string
    By weezerboy in forum Macromedia ColdFusion
    Replies: 1
    Last Post: February 16th, 09:56 PM
  4. Replace value in a string
    By Eric in forum PHP Development
    Replies: 5
    Last Post: October 29th, 02:36 PM
  5. Replies: 4
    Last Post: September 12th, 04:19 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139