function.parse-url.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 244 行

HTML
244
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Parse a URL and return its components</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.http-build-query.html">http_build_query</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.rawurldecode.html">rawurldecode</a></div> <div class="up"><a href="ref.url.html">URL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.parse-url" class="refentry"> <div class="refnamediv">  <h1 class="refname">parse_url</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">parse_url</span> &mdash; <span class="dc-title">Parse a URL and return its components</span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><b><b>parse_url</b></b></span>    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$url</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$component</tt></span>  ] )</div>  <p class="para rdfs-comment">   This function parses a URL and returns an associative array containing any   of the various components of the URL that are present.  </p>  <p class="para">   This function is <em class="emphasis">not</em> meant to validate   the given URL, it only breaks it up into the above listed parts. Partial   URLs are also accepted, <b>parse_url()</b> tries its best to   parse them correctly.  </p> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">url</tt></i></span>     <dd>      <p class="para">       The URL to parse      </p>     </dd>    </dt>   </dl>   <dl>    <dt>     <span class="term"><i><tt class="parameter">component</tt></i></span>     <dd>      <p class="para">       Specify one of <b><tt>PHP_URL_SCHEME</tt></b>,       <b><tt>PHP_URL_HOST</tt></b>, <b><tt>PHP_URL_PORT</tt></b>,       <b><tt>PHP_URL_USER</tt></b>, <b><tt>PHP_URL_PASS</tt></b>,       <b><tt>PHP_URL_PATH</tt></b>, <b><tt>PHP_URL_QUERY</tt></b>       or <b><tt>PHP_URL_FRAGMENT</tt></b> to retrieve just a specific       URL component as a <a href="language.types.string.html" class="type string">string</a>.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   On seriously malformed URLs, <b>parse_url()</b> may return   <b><tt>FALSE</tt></b> and emit a <b><tt>E_WARNING</tt></b>. Otherwise an associative   array is returned, whose components may be (at least one):   <ul class="itemizedlist">    <li class="listitem">     <span class="simpara">      <var class="varname">scheme</var> - e.g. http     </span>    </li>    <li class="listitem">     <span class="simpara">      <var class="varname">host</var>      </span>    </li>    <li class="listitem">     <span class="simpara">      <var class="varname">port</var>     </span>    </li>    <li class="listitem">     <span class="simpara">      <var class="varname">user</var>     </span>    </li>    <li class="listitem">     <span class="simpara">      <var class="varname">pass</var>     </span>    </li>    <li class="listitem">     <span class="simpara">      <var class="varname">path</var>     </span>    </li>    <li class="listitem">     <span class="simpara">      <var class="varname">query</var> - after the question mark <i>?</i>     </span>    </li>    <li class="listitem">     <span class="simpara">      <var class="varname">fragment</var> - after the hashmark <i>#</i>     </span>    </li>   </ul>  </p>  <p class="para">   If the <i><tt class="parameter">component</tt></i> parameter is specified a   <a href="language.types.string.html" class="type string">string</a> is returned instead of an <a href="language.types.array.html" class="type array">array</a>.  </p> </div> <div class="refsect1 changelog">  <h3 class="title">ChangeLog</h3>  <p class="para">   <table class="informaltable">    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Version</th>       <th colspan="1">Description</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">5.1.2</td>       <td colspan="1" rowspan="1" align="left">Added the <i><tt class="parameter">component</tt></i> parameter</td>      </tr>     </tbody>    </colgroup>   </table>  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 A <b>parse_url()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'http://username:password@hostname/path?arg=value#anchor'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">parse_url</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">));<br /><br />echo&nbsp;</span><span style="color: #0000BB">parse_url</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_URL_PATH</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>The above example will output:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>Array(    [scheme] =&gt; http    [host] =&gt; hostname    [user] =&gt; username    [pass] =&gt; password    [path] =&gt; /path    [query] =&gt; arg=value    [fragment] =&gt; anchor)/path</pre></div>    </pre></div>   </div>  </p> </div> <div class="refsect1 notes">  <h3 class="title">Notes</h3>  <blockquote><p><b class="note">Note</b>:        This function doesn&#039;t work with relative URLs.   <br />  </p></blockquote>  <blockquote><p><b class="note">Note</b>:        This function is intended specifically for the purpose of parsing URLs    and not URIs. However, to comply with PHP&#039;s backwards compatibility    requirements it makes an exception for the file:// scheme where tripple    slashes (file:///...) are allowed. For any other scheme this is invalid.   <br />  </p></blockquote> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.pathinfo.html" class="function" rel="rdfs-seeAlso">pathinfo()</a></li>    <li class="member"><a href="function.parse-str.html" class="function" rel="rdfs-seeAlso">parse_str()</a></li>    <li class="member"><a href="function.http-build-query.html" class="function" rel="rdfs-seeAlso">http_build_query()</a></li>    <li class="member"><a href="function.dirname.html" class="function" rel="rdfs-seeAlso">dirname()</a></li>    <li class="member"><a href="function.basename.html" class="function" rel="rdfs-seeAlso">basename()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.http-build-query.html">http_build_query</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.rawurldecode.html">rawurldecode</a></div> <div class="up"><a href="ref.url.html">URL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?