Connecting Tech Pros Worldwide Forums | Help | Site Map

Help with string replace...

Tihon
Guest
 
Posts: n/a
#1: Jul 17 '05
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
Guest
 
Posts: n/a
#2: Jul 17 '05

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

Regards.

Guillaume
Guillaume Brocker
Guest
 
Posts: n/a
#3: Jul 17 '05

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

Regards.

Guillaume
Martin Lucas-Smith
Guest
 
Posts: n/a
#4: Jul 17 '05

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

Pedro Graca
Guest
 
Posts: n/a
#5: Jul 17 '05

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()?
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 =--
Justin Koivisto
Guest
 
Posts: n/a
#6: Jul 17 '05

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
Tihon
Guest
 
Posts: n/a
#7: Jul 17 '05

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!
Tihon
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Help with string replace...


Pedro Graca <hexkid@hotpop.com> wrote in message news:<bvtsh8$1042bj$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!
Closed Thread