获取第一次出现正斜杠后的字符串
我有一个字符串:
$uri = "start/test/go/";
基本上我需要知道可以使用哪个正则表达式和 PHP 函数来将第一个项目与正斜杠(“/”)匹配并将其从字符串中删除。如果第一个项目没有启动并且是其他可能有空格的项目,它也应该起作用。 所以所有这些组合都应该有效:
$uri = "start_my_test/test/go/";
$uri2 = "start my test/test/go/";
然后在正则表达式之后它应该总是返回:
$newUri = "test/go/";
哦,字符串的另一边也可以是任何东西,所以基本上我希望它在第一次出现正斜杠之前删除任何内容。
I have a string:
$uri = "start/test/go/";
Basically I need to know which regular expression and PHP function I can use to match the first item with a forward slash ("/") and remove it from the string. It should also work if the first item is not start and is anything else which might also have a space in it.
So all these combination should work:
$uri = "start_my_test/test/go/";
$uri2 = "start my test/test/go/";
Then after the RegEx it should always return:
$newUri = "test/go/";
Oh and the other side of the string could be anything as well, So basically I want it to delete anything before the first occurrence of a forward slash.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
strstr
查找字符串在 php.ini 中第一次出现的位置。它本身应该返回字符串的其余部分。
参见此处
Use
strstr
to find the first occurrence of a string in php.That in itself should return the remainder of the string.
see here
这表示“从字符串的开头开始”
^
,“匹配不是正斜杠的任意数量的字符”[^\/]*
,然后匹配单正斜杠\/
- 以及“将整个匹配的内容替换为空”''
。This says "start at the beginning of the string"
^
, "match any number of characters that are not a forward slash"[^\/]*
, then match a single forward slash\/
-- and "replace the whole matched thing with nothing"''
.正则表达式对于您的需要来说太昂贵了。使用
strpos
和substr
代替regex is too expensive an operation for what you need. use
strpos
andsubstr
instead