As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center提供指导。




已关闭8年。




我有这种格式的字符串/abcd-efg-sms-3-topper-2
我想从中删除第一个/字符。

我知道我可以使用substr()函数删除它,但是我不想使用它,因为我首先必须检查第一个字符是否为斜杠。是否有其他方法可以删除斜线而无需先检查斜线,并使其具有合理的性能(即避免复杂的常规表达式)?

最佳答案

使用装饰:

$string = '/abcd-efg-sms-3-topper-2';
echo ltrim($string, '/');
// abcd-efg-sms-3-topper-2
echo rtrim($string, '/');
// /abcd-efg-sms-3-topper-2
echo trim($string, '/');
// abcd-efg-sms-3-topper-2

10-07 17:48