Php 如何在第4和第5斜杠之间获得url的一部分?
请帮忙Php 如何在第4和第5斜杠之间获得url的一部分?,php,regex,Php,Regex,请帮忙 我正在学习PHP。我有一个url:'',如何仅从带有正则表达式(可能是preg_replace)的字符串中获取'123a'。我想不出来。提前谢谢 试试这个正则表达式: ^(?:[^/]*/){4}([^/]+) preg_match('/^(?:[^\\/]*\\/){4}([^\\/]+)/', $str, $match) $match[1]将保存您的123a为什么要使用正则表达式 $parts = explode('/', 'http://example.com/articles
我正在学习PHP。我有一个url:'',如何仅从带有正则表达式(可能是preg_replace)的字符串中获取'123a'。我想不出来。提前谢谢 试试这个正则表达式:
^(?:[^/]*/){4}([^/]+)
preg_match('/^(?:[^\\/]*\\/){4}([^\\/]+)/', $str, $match)
$match[1]
将保存您的123a
为什么要使用正则表达式
$parts = explode('/', 'http://example.com/articles/123a/view');
echo $parts[4];
我键入了以下内容:preg_replace(“^(?:[^/]*/){4}([^/]+)”,“,)@Anderson83:您忘记了分隔符。
explode
的第三个参数允许您设置限制。您可以将其设置为6
,以防止一些可能的多余爆炸。