Suppose we have such a variable:

$a = "user/category/link/buy/" 

We need to make this option from this:

 $a = "usercategorylinkbuy/" 

Developments:

 strtr($a, array('/' => '')); //Удаляет абсолютно все / внутри переменной 

And if in the variable $ a initially will not be "/", then it should not be added to the end.

  • one
    I love this approach: strtr ($ a, array ('/' => '')); // patsans, I tried to type, what? - Zowie
  • one
    [Regular expressions] [1] [1]: php.su/functions/?preg-replace - triplustri
  • 2
    On a pearl it would be s|/([^/]+)(?=/)|\1|g - alexlz

2 answers 2

If you know that there is exactly / at the end of a variable, then the following code makes sense:

 if(substr($a,-1)=="/") { $a = str_replace("/","",$a); $a .= "/"; } else { $a = str_replace("/","",$a); } 
  • A little Indian, but there will be a desire - you will optimize ... This is so, in a hurry - oxyage

Everything is much simpler.

 trim($a, '/'); 

True, he will cut off at the beginning, if at the beginning you need to save '/' you can do this:

 '/'.trim($a, '/');