这是我可以破解的东西,但我想知道是否有人对我的问题有一个干净的解决方案。我拼凑的东西不一定非常简洁或快速!
我有一个像这样的字符串 ///hello/world///
。我只需要去掉第一个和最后一个斜线,其他的都不需要,这样我就得到了这样的字符串 //hello/world//
。
PHP 的 trim
不太正确:执行 trim($string, '/')
将返回 hello/world
。
需要注意的是字符串的开头或结尾不一定有任何斜线。以下是我希望对不同字符串进行处理的几个示例:
///hello/world/// > //hello/world//
/hello/world/// > hello/world//
hello/world/ > hello/world
在此先感谢您的帮助!
最佳答案
我首先想到的是:
if ($string[0] == '/') $string = substr($string,1);
if ($string[strlen($string)-1] == '/') $string = substr($string,0,strlen($string)-1);
关于php - 仅修剪字符串中第一次和最后一次出现的字符 (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3834319/