简体   繁体   中英

Simple php regex question

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.

Cheers

Use strstr to find the first occurrence of a string in php.

That in itself should return the remainder of the string.

see here

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

这表示“从字符串的开头开始” ^ ,“匹配任意数量的非正斜杠字符” [^\\/]* ,然后匹配单个正斜杠\\/ -并“替换整个匹配的东西一无所有” ''

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM