473,499 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!
Jul 17 '05 #1
7 2754
Tihon wrote:
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!


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
Jul 17 '05 #2
Tihon wrote:
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!


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
Jul 17 '05 #3


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'm unclear why a simple "<a href="../"> won't work, instead of trying to
compute what the reference should be.
Martin

Jul 17 '05 #4
Tihon wrote:
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/

(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 =--
Jul 17 '05 #5
Martin Lucas-Smith wrote:
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'm unclear why a simple "<a href="../"> won't work, instead of trying to
compute what the reference should be.


using mod_rewrite may be one good reason...

--
Justin Koivisto - sp**@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
Jul 17 '05 #6
> Tihon wrote:
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/

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


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!
Jul 17 '05 #7
Pedro Graca <he****@hotpop.com> wrote in message news:<bv*************@ID-203069.news.uni-berlin.de>...
Tihon wrote:
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/

(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 previous post does not seem to appear,
Thanks to all of you, thats exactly what i needed!
Thank you!
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2055
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
4
308
by: Andrew | last post by:
I am a newbie to C# and would appreciate any help converting the following javascript function to C# function fxnParseIt() { // Note: sInputString code for demo purposes only, and should be // ...
5
11337
by: Mahesha | last post by:
Hello, I need help in replacing one string pattern with another. Ex: I have a financial security expression like log(T 3.25 6/24/2004)/sqrt(T 4.5 6/19/2002) Here "T 3.25 6/24/2004" is a...
1
3692
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
22
2142
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being...
1
2000
by: Random Task | last post by:
Can someone help me by providing an example of how to replace / with \ in a string in xslt2. The characters / and \ seem to cause me grief ... I am trying the below code currently ... Any...
31
4545
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
2
2357
by: Alex Maghen | last post by:
This is a bit of an abuse of this group. Just a nit, but I'm hoping someone really good with Regular Expressions can help me out here. I need to write a regular expression that will do the...
4
2782
by: sandvet03 | last post by:
I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to...
5
4901
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.