获取第一次出现正斜杠后的字符串

发布于 2024-10-03 11:49:56 字数 395 浏览 0 评论 0原文

我有一个字符串:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒几许 2024-10-10 11:49:57

使用 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

暮色兮凉城 2024-10-10 11:49:57
$result = preg_replace('/^[^\/]*\//' , '', $subject);

这表示“从字符串的开头开始”^,“匹配不是正斜杠的任意数量的字符”[^\/]*,然后匹配单正斜杠 \/ - 以及“将整个匹配的内容替换为空”''

$result = preg_replace('/^[^\/]*\//' , '', $subject);

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" ''.

帥小哥 2024-10-10 11:49:57

正则表达式对于您的需要来说太昂贵了。使用 strpossubstr 代替

$position = strpos($needle, $haystack);
if ( $position !== false ) {
  $result = substr($needle, $position + 1);
}

regex is too expensive an operation for what you need. use strpos and substr instead

$position = strpos($needle, $haystack);
if ( $position !== false ) {
  $result = substr($needle, $position + 1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文