使用PHP获取url中的内容

时间:2017-03-13 17:20:00

标签: php regex preg-match preg-split

我对正则表达式相当新,我遇到了一些问题。我希望网址在/?//之间。

示例:

www.domain.com/about?page/readmore/party

我想要的值:

about page readmore party

我怎样才能做到这一点?试图用preg_split来做,但无法实现。

2 个答案:

答案 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>