{"id":101362,"date":"2021-09-01T08:41:46","date_gmt":"2021-09-01T00:41:46","guid":{"rendered":"https:\/\/www.keaglegz.com\/101362.html"},"modified":"2026-06-30T17:29:13","modified_gmt":"2026-06-30T09:29:13","slug":"php%e8%a7%a3%e6%9e%90url%e5%b9%b6%e5%be%97%e5%88%b0url%e4%b8%ad%e7%9a%84%e5%8f%82%e6%95%b0%e5%8f%8a%e8%8e%b7%e5%8f%96url%e5%8f%82%e6%95%b0%e7%9a%84%e5%9b%9b%e7%a7%8d%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.dongwubaike.cn\/fanhao\/101362.html","title":{"rendered":"PHP\u89e3\u6790url\u5e76\u5f97\u5230url\u4e2d\u7684\u53c2\u6570\u53ca\u83b7\u53d6url\u53c2\u6570\u7684\u56db\u79cd\u65b9\u5f0f"},"content":{"rendered":"\n<p>\u672c\u6587\u7ed9\u5927\u5bb6\u4ecb\u7ecdphp\u89e3\u6790url\u5e76\u5f97\u5230url\u4e2d\u7684\u53c2\u6570\u53ca\u83b7\u53d6url\u53c2\u6570\u7684\u56db\u79cd\u65b9\u5f0f\uff0c\u6d89\u53ca\u5230\u5c06\u5b57\u7b26\u4e32\u53c2\u6570\u53d8\u4e3a\u6570\u7ec4\uff0c\u5c06\u53c2\u6570\u53d8\u4e3a\u5b57\u7b26\u4e32\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u672c\u6587\u4ee3\u7801\u7b80\u5355\u6613\u61c2\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u4e00\u8d77\u770b\u770b\u5427<br \/> \u4e0b\u9762\u4e00\u6bb5\u4ee3\u7801\u662fphp\u89e3\u6790url\u5e76\u5f97\u5230url\u4e2d\u7684\u53c2\u6570\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>  &lt;?php  $url = &#x27;http:\/\/www.baidu.com\/index.php?m=content&amp;c=index&amp;a=lists&amp;catid=6&amp;area=0&amp;author=0&amp;h=0&amp;region=0&amp;s=1&amp;page=1&#x27;;  $arr = parse_url($url);  var_dump($arr);  $arr_query = convertUrlQuery($arr[&#x27;query&#x27;]);  var_dump($arr_query);  var_dump(getUrlQuery($arr_query));  \/**   * \u5c06\u5b57\u7b26\u4e32\u53c2\u6570\u53d8\u4e3a\u6570\u7ec4   * @param $query   * @return array array (size=10)            &#x27;m&#x27; =&gt; string &#x27;content&#x27; (length=7)            &#x27;c&#x27; =&gt; string &#x27;index&#x27; (length=5)            &#x27;a&#x27; =&gt; string &#x27;lists&#x27; (length=5)            &#x27;catid&#x27; =&gt; string &#x27;6&#x27; (length=1)            &#x27;area&#x27; =&gt; string &#x27;0&#x27; (length=1)            &#x27;author&#x27; =&gt; string &#x27;0&#x27; (length=1)            &#x27;h&#x27; =&gt; string &#x27;0&#x27; (length=1)            &#x27;region&#x27; =&gt; string &#x27;0&#x27; (length=1)            &#x27;s&#x27; =&gt; string &#x27;1&#x27; (length=1)            &#x27;page&#x27; =&gt; string &#x27;1&#x27; (length=1)   *\/  function convertUrlQuery($query)  {    $queryParts = explode(&#x27;&amp;&#x27;, $query);    $params = array();    foreach ($queryParts as $param) {      $item = explode(&#x27;=&#x27;, $param);      $params[$item[0]] = $item[1];    }    return $params;  }  \/**   * \u5c06\u53c2\u6570\u53d8\u4e3a\u5b57\u7b26\u4e32   * @param $array_query   * @return string string &#x27;m=content&amp;c=index&amp;a=lists&amp;catid=6&amp;area=0&amp;author=0&amp;h=0&amp;region=0&amp;s=1&amp;page=1&#x27; (length=73)   *\/  function getUrlQuery($array_query)  {    $tmp = array();    foreach($array_query as $k=&gt;$param)    {      $tmp[] = $k.&#x27;=&#x27;.$param;    }    $params = implode(&#x27;&amp;&#x27;,$tmp);    return $params;  }  <\/pre>\n<p>\u4e0b\u9762\u901a\u8fc7\u56db\u79cd\u5b9e\u4f8b\u7ed9\u5927\u5bb6\u4ecb\u7ecdphp url \u53c2\u6570\u83b7\u53d6\u65b9\u5f0f\u3002<br \/> \u5728\u5df2\u77e5URL\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u81ea\u8eab\u60c5\u51b5\u91c7\u7528$_GET\u6765\u83b7\u53d6\u76f8\u5e94\u7684\u53c2\u6570\u4fe1\u606f\uff08$_GET[&#8216;name&#8217;]\uff09;\u90a3\uff0c\u5728\u672a\u77e5\u60c5\u51b5\u4e0b\u5982\u4f55\u83b7\u53d6\u5230URL\u4e0a\u7684\u53c2\u6570\u4fe1\u606f\u5462\uff1f<\/p>\n<h2>\u7b2c\u4e00\u79cd\u3001\u5229\u7528$_SERVER\u5185\u7f6e\u6570\u7ec4\u53d8\u91cf<\/h2>\n<p>\u76f8\u5bf9\u8f83\u4e3a\u539f\u59cb\u7684$_SERVER[&#8216;QUERY_STRING&#8217;]\u6765\u83b7\u53d6\uff0cURL\u7684\u53c2\u6570\uff0c\u901a\u5e38\u4f7f\u7528\u8fd9\u4e2a\u53d8\u91cf\u8fd4\u56de\u7684\u4f1a\u662f\u7c7b\u4f3c\u8fd9\u6837\u7684\u6570\u636e\uff1aname=tank&#038;sex=1<br \/> \u5982\u679c\u9700\u8981\u5305\u542b\u6587\u4ef6\u540d\u7684\u8bdd\u53ef\u4ee5\u4f7f\u7528$_SERVER[&#8220;REQUEST_URI&#8221;](\u8fd4\u56de\u7c7b\u4f3c\uff1a\/index.php?name=tank&#038;sex=1)<\/p>\n<h2>\u7b2c\u4e8c\u79cd\u3001\u5229\u7528pathinfo\u5185\u7f6e\u51fd\u6570<\/h2>\n<p>\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre>  &lt;?php  $test = pathinfo(&quot;http:\/\/localhost\/index.php&quot;);  print_r($test);  <\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b<\/p>\n<pre>  Array  (     [dirname] =&gt; http:\/\/localhost \/\/url\u7684\u8def\u5f84     [basename] =&gt; index.php \/\/\u5b8c\u6574\u6587\u4ef6\u540d     [extension] =&gt; php \/\/\u6587\u4ef6\u540d\u540e\u7f00     [filename] =&gt; index \/\/\u6587\u4ef6\u540d  )  ?&gt;  <\/pre>\n<\/h2>\n<p>\u7b2c\u4e09\u79cd\u3001\u5229\u7528parse_url\u5185\u7f6e\u51fd\u6570<\/h2>\n<p>\u4ee3\u7801\u5982\u4e0b:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dongwubaike.cn\/fanhao\/wp-content\/uploads\/2026\/06\/4e1cada8bb0345fbbe8b949e705d9267.webp\" alt=\"\"><\/p>\n<pre>  &lt;?php  $test = parse_url(&quot;http:\/\/localhost\/index.php?name=tank&amp;sex=1#top&quot;);  print_r($test);  <\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b<\/p>\n<pre>  Array  (     [scheme] =&gt; http \/\/\u4f7f\u7528\u4ec0\u4e48\u534f\u8bae     [host] =&gt; localhost \/\/\u4e3b\u673a\u540d     [path] =&gt; \/index.php \/\/\u8def\u5f84     [query] =&gt; name=tank&amp;sex=1 \/\/ \u6240\u4f20\u7684\u53c2\u6570     [fragment] =&gt; top \/\/\u540e\u9762\u6839\u7684\u951a\u70b9  )  ?&gt;  <\/pre>\n<\/h2>\n<p>\u7b2c\u56db\u79cd\u3001\u5229\u7528basename\u5185\u7f6e\u51fd\u6570<\/h2>\n<p>\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre>  &lt;?php  $test = basename(&quot;http:\/\/localhost\/index.php?name=tank&amp;sex=1#top&quot;);  echo $test;  <\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b<\/p>\n<pre>  index.php?name=tank&amp;sex=1#top  ?&gt;  <\/pre>\n<p>\u53e6\u5916\uff0c\u8fd8\u6709\u5c31\u662f\u81ea\u5df1\u901a\u8fc7\u6b63\u5219\u5339\u914d\u7684\u5904\u7406\u65b9\u5f0f\u6765\u83b7\u53d6\u9700\u8981\u7684\u503c\u4e86\u3002\u8fd9\u79cd\u65b9\u5f0f\u8f83\u4e3a\u7cbe\u786e\uff0c\u6548\u7387\u6682\u4e0d\u8003\u8651\u3002\u3002\u3002<br \/> \u4e0b\u9762\u62d3\u5c55\u5b9e\u8df5\u4e0b\u6b63\u5219\u5904\u7406\u65b9\u5f0f\uff1a<br \/> \u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre>  &lt;?php  preg_match_all(&quot;\/(w+=w+)(#w+)?\/i&quot;,&quot;http:\/\/localhost\/index.php?name=tank&amp;sex=1#top&quot;,$match);  print_r($match);  <\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b<\/p>\n<pre>  Array  (    [0] =&gt; Array      (        [0] =&gt; name=tank        [1] =&gt; sex=1#top      )    [1] =&gt; Array       (        [0] =&gt; name=tank         [1] =&gt; sex=1       )     [2] =&gt; Array      (         [0] =&gt;        [1] =&gt; #top      )  )  ?&gt;  <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u7ed9\u5927\u5bb6\u4ecb\u7ecdphp\u89e3\u6790url\u5e76\u5f97\u5230url\u4e2d\u7684\u53c2\u6570\u53ca\u83b7\u53d6url\u53c2\u6570\u7684\u56db\u79cd\u65b9\u5f0f\uff0c\u6d89\u53ca\u5230\u5c06\u5b57\u7b26\u4e32\u53c2\u6570\u53d8\u4e3a\u6570\u7ec4\uff0c\u5c06\u53c2\u6570\u53d8\u4e3a\u5b57\u7b26\u4e32\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u672c\u6587\u4ee3\u7801\u7b80\u5355\u6613\u61c2\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u4e00\u8d77\u770b\u770b\u5427<br \/>\n \u4e0b\u9762\u4e00\u6bb5\u4ee3\u7801\u662fphp\u89e3\u6790url\u5e76\u5f97\u5230url\u4e2d\u7684\u53c2\u6570\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-101362","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/101362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/comments?post=101362"}],"version-history":[{"count":1,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/101362\/revisions"}],"predecessor-version":[{"id":284155,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/posts\/101362\/revisions\/284155"}],"wp:attachment":[{"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/media?parent=101362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/categories?post=101362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dongwubaike.cn\/fanhao\/wp-json\/wp\/v2\/tags?post=101362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}