我对正则表达式相当新,我遇到了一些问题。我希望网址在/
和?
或/
和/
之间。
示例:
www.domain.com/about?page/readmore/party
我想要的值:
about
page
readmore
party
我怎样才能做到这一点?试图用preg_split
来做,但无法实现。
答案 0 :(得分:2)
这意味着您将?
字符替换为/
以获取www.domain.com/about/page/readmore/party
,然后按/
拆分并获取[www.domain.com, about, page, readmore, party]
;然后你只用array_slice提取除第一部分之外的所有部分:
$url = 'www.domain.com/about?page/readmore/party';
$parts = array_slice( explode ('/', str_replace ('?', '/', $url )), 1);
var_dump( $parts );
输出:
array(4) {
[0]=>
string(5) "about"
[1]=>
string(4) "page"
[2]=>
string(8) "readmore"
[3]=>
string(5) "party"
}
查看实时here
答案 1 :(得分:0)
$urlStr = "www.domain.com/about?page/readmore/party";<br>
$newStr = str_replace("?","/",$urlStr);<br>
$newRep = explode("/",$newStr);<br>
$newArr = [];<br>
for($i = 0; $i < count($newRep); $i++){<br>
if($i == 0){<br>
<br>
}else{<br>
array_push($newArr,$newRep[$i]);<br>
}<br>
}<br>
print_r($newArr);<br>