mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 05:26:57 +08:00
267 lines
11 KiB
HTML
267 lines
11 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
<html>
|
||
<head>
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
<title>解析 URL,返回其组成部分</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.parse-url" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">parse_url</h1>
|
||
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">parse_url</span> — <span class="dc-title">解析 URL,返回其组成部分</span></p>
|
||
|
||
</div>
|
||
|
||
<div class="refsect1 description" id="refsect1-function.parse-url-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<span class="methodname"><strong>parse_url</strong></span>
|
||
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$url</code></span>
|
||
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$component</code><span class="initializer"> = -1</span></span>
|
||
] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
本函数解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分。
|
||
</p>
|
||
<p class="para">
|
||
本函数<em class="emphasis">不是</em>用来验证给定 URL
|
||
的合法性的,只是将其分解为下面列出的部分。不完整的 URL
|
||
也被接受,<span class="function"><strong>parse_url()</strong></span> 会尝试尽量正确地将其解析。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 parameters" id="refsect1-function.parse-url-parameters">
|
||
<h3 class="title">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">url</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
要解析的 URL。无效字符将使用
|
||
<em>_</em> 来替换。
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">component</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
指定 <strong><code>PHP_URL_SCHEME</code></strong>、
|
||
<strong><code>PHP_URL_HOST</code></strong>、 <strong><code>PHP_URL_PORT</code></strong>、
|
||
<strong><code>PHP_URL_USER</code></strong>、 <strong><code>PHP_URL_PASS</code></strong>、
|
||
<strong><code>PHP_URL_PATH</code></strong>、 <strong><code>PHP_URL_QUERY</code></strong>
|
||
或 <strong><code>PHP_URL_FRAGMENT</code></strong> 的其中一个来获取 URL 中指定的部分的 <span class="type"><a href="language.types.string.html" class="type string">string</a></span>。 (除了指定为 <strong><code>PHP_URL_PORT</code></strong> 后,将返回一个 <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> 的值)。
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.parse-url-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
对严重不合格的 URL,<span class="function"><strong>parse_url()</strong></span> 可能会返回
|
||
<strong><code>FALSE</code></strong>。
|
||
</p>
|
||
<p class="para">
|
||
如果省略了 <code class="parameter">component</code> 参数,将返回一个关联数组 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>,在目前至少会有一个元素在该数组中。数组中可能的键有以下几种:
|
||
<ul class="itemizedlist">
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<var class="varname"><var class="varname">scheme</var></var> - 如 http
|
||
</span>
|
||
</li>
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<var class="varname"><var class="varname">host</var></var>
|
||
</span>
|
||
</li>
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<var class="varname"><var class="varname">port</var></var>
|
||
</span>
|
||
</li>
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<var class="varname"><var class="varname">user</var></var>
|
||
</span>
|
||
</li>
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<var class="varname"><var class="varname">pass</var></var>
|
||
</span>
|
||
</li>
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<var class="varname"><var class="varname">path</var></var>
|
||
</span>
|
||
</li>
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<var class="varname"><var class="varname">query</var></var> - 在问号 <em>?</em> 之后
|
||
</span>
|
||
</li>
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<var class="varname"><var class="varname">fragment</var></var> - 在散列符号 <em>#</em> 之后
|
||
</span>
|
||
</li>
|
||
</ul>
|
||
</p>
|
||
<p class="para">
|
||
如果指定了 <code class="parameter">component</code> 参数,
|
||
<span class="function"><strong>parse_url()</strong></span> 返回一个 <span class="type"><a href="language.types.string.html" class="type string">string</a></span> (或在指定为 <strong><code>PHP_URL_PORT</code></strong> 时返回一个 <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>)而不是 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>。如果 URL 中指定的组成部分不存在,将会返回 <strong><code>NULL</code></strong>。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
|
||
<div class="refsect1 changelog" id="refsect1-function.parse-url-changelog">
|
||
<h3 class="title">更新日志</h3>
|
||
<p class="para">
|
||
<table class="doctable informaltable">
|
||
|
||
<thead>
|
||
<tr>
|
||
<th>版本</th>
|
||
<th>说明</th>
|
||
</tr>
|
||
|
||
</thead>
|
||
|
||
<tbody class="tbody">
|
||
<tr>
|
||
<td>5.4.7</td>
|
||
<td>
|
||
修复了 <em class="emphasis">host</em> 在 <em class="emphasis">协议</em> 省略时的识别。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>5.3.3</td>
|
||
<td>
|
||
在 URL 解析失败时将不会产生 <strong><code>E_WARNING</code></strong> 级别的错误。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>5.1.2</td>
|
||
<td>增加了参数 <code class="parameter">component</code>。</td>
|
||
</tr>
|
||
|
||
</tbody>
|
||
|
||
</table>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 examples" id="refsect1-function.parse-url-examples">
|
||
<h3 class="title">范例</h3>
|
||
<p class="para">
|
||
<div class="example" id="example-4891">
|
||
<p><strong>Example #1 <span class="function"><strong>parse_url()</strong></span> 例子</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$url </span><span style="color: #007700">= </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 </span><span style="color: #0000BB">parse_url</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_URL_PATH</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
<div class="example-contents"><p>以上例程会输出:</p></div>
|
||
<div class="example-contents screen">
|
||
<div class="cdata"><pre>
|
||
Array
|
||
(
|
||
[scheme] => http
|
||
[host] => hostname
|
||
[user] => username
|
||
[pass] => password
|
||
[path] => /path
|
||
[query] => arg=value
|
||
[fragment] => anchor
|
||
)
|
||
/path
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</p>
|
||
<p class="para">
|
||
<div class="example" id="example-4892">
|
||
<p><strong>Example #2 <span class="function"><strong>parse_url()</strong></span> 解析丢失协议的例子</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$url </span><span style="color: #007700">= </span><span style="color: #DD0000">'//www.example.com/path?googleguy=googley'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// 在 5.4.7 之前这会输出路径 "//www.example.com/path"<br /></span><span style="color: #0000BB">var_dump</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 /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
<div class="example-contents"><p>以上例程会输出:</p></div>
|
||
<div class="example-contents screen">
|
||
<div class="cdata"><pre>
|
||
array(3) {
|
||
["host"]=>
|
||
string(15) "www.example.com"
|
||
["path"]=>
|
||
string(5) "/path"
|
||
["query"]=>
|
||
string(17) "googleguy=googley"
|
||
}
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 notes" id="refsect1-function.parse-url-notes">
|
||
<h3 class="title">注释</h3>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
本函数不能用于相对 URL。
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
<span class="function"><strong>parse_url()</strong></span> 是专门用来解析 URL 而不是 URI 的。不过为遵从 PHP
|
||
向后兼容的需要有个例外,对 file:// 协议允许三个斜线(file:///...)。其它任何协议都不能这样。
|
||
</p>
|
||
</p></blockquote>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 seealso" id="refsect1-function.parse-url-seealso">
|
||
<h3 class="title">参见</h3>
|
||
<p class="para">
|
||
<ul class="simplelist">
|
||
<li class="member"><span class="function"><a href="pathinfo.html" class="function" rel="rdfs-seeAlso">pathinfo()</a> - 返回文件路径的信息</span></li>
|
||
<li class="member"><span class="function"><a href="parse_str.html" class="function" rel="rdfs-seeAlso">parse_str()</a> - 将字符串解析成多个变量</span></li>
|
||
<li class="member"><span class="function"><a href="http_build_query.html" class="function" rel="rdfs-seeAlso">http_build_query()</a> - 生成 URL-encode 之后的请求字符串</span></li>
|
||
<li class="member"><span class="function"><strong>http_build_url()</strong></span></li>
|
||
<li class="member"><span class="function"><a href="dirname.html" class="function" rel="rdfs-seeAlso">dirname()</a> - 返回路径中的目录部分</span></li>
|
||
<li class="member"><span class="function"><a href="basename.html" class="function" rel="rdfs-seeAlso">basename()</a> - 返回路径中的文件名部分</span></li>
|
||
<li class="member"><a href="http://www.faqs.org/rfcs/rfc3986" class="link external">» RFC 3986</a></li>
|
||
</ul>
|
||
</p>
|
||
</div>
|
||
|
||
</div></div></div></body></html> |