tags:

views:

65

answers:

2

how to replace slashes from beginning and end?

So for example, all of these:

/this/that/

/this/that

this/that/

////////this/that////////////

... become: this/that

A: 

Use ltrim and rtrim:

$txt = '////////this/that////////////';
echo rtrim(ltrim($txt, '/'), '/');

Result:

this/that
Sarfraz
This answer is not incorrect. Don't understand the down-votes.
sshow
Why the down votes please?
Sarfraz
@sshow I didn't downvote, but I think it is because the answer isn't quite as good as it could have been (e.g. it uses `rtrim` and `ltrim` rather than trim)
Yacoby
@Yacoby: Thanks for your comment but I think answer isn't **wrong**. Doesn't deserve downvote if no upvote at all :)
Sarfraz
The title to the down-vote is "this answer is *not* useful", which this answer obviously is, since it answers the question.
sshow
This code is self-obfuscated. Being unobvious it makes reading and supporting harder.
Col. Shrapnel
@sshow In my opinion it "is not useful" because it doesn't do it in the "best possible" way. I didn't downvote as I am clearly biased on this issue.
Yacoby
Works just the same just bit more code. So technically its not wrong.
+10  A: 

Use trim with the second argument the character(s) that you want to trim.

$result = trim('/this/that//', '/');
//$result is now 'this/that'
Yacoby