php 删除链接中的第一个正斜杠?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/955212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove first forward slash in a link?
提问by 0plus1
I need to remove the first forward slash inside link formatted like this:
我需要删除格式如下的链接内的第一个正斜杠:
/directory/link.php
I need to have:
我需要:
directory/link.php
I'm not literate in regular expressions (preg_replace?) and those slashes are killing me..
我不了解正则表达式(preg_replace?),那些斜线正在杀死我..
I need your help stackoverflow!
我需要你的帮助 stackoverflow!
Thank you very much!
非常感谢!
回答by Stefan Gehrig
Just because nobody has mentioned it before:
只是因为之前没有人提到过:
$uri = "/directory/link.php";
$uri = ltrim($uri, '/');
The benefit of this one is:
这样做的好处是:
compared to the
substr()solution:it works also with paths that do not start with a slash. So using the same procedure multiple times on an uri is safe.compared to the
preg_replace()solution:it's certainly much more faster. Actuating the regex-engine for such a trivial task is, in my opinion, overkill.
与
substr()解决方案相比:它也适用于不以斜杠开头的路径。因此,在 uri 上多次使用相同的过程是安全的。与
preg_replace()解决方案相比:它肯定要快得多。在我看来,为这样一个微不足道的任务启动正则表达式引擎是矫枉过正的。
回答by duckyflip
preg_replace('/^\//', '', $link);
回答by karim79
If it's always the first character, you won't need a regex:
如果它始终是第一个字符,则不需要正则表达式:
$uri = "/directory/link.php";
$uri = substr($uri, 1);

