<!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>Parse a URL and return its components</title> </head> <body><div class="manualnavbar" 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> — <span class="dc-title">Parse a URL and return its components</span></p> </div> <div class="refsect1 description" id="refsect1-function.parse-url-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"><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> ] )</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, <span class="function"><strong>parse_url()</strong></span> tries its best to parse them correctly. </p> </div> <div class="refsect1 parameters" id="refsect1-function.parse-url-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">url</code></em></span> <dd> <p class="para"> The URL to parse. Invalid characters are replaced by <em>_</em>. </p> </dd> </dt> </dl> <dl> <dt> <span class="term"><em><code class="parameter">component</code></em></span> <dd> <p class="para"> Specify one of <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> or <strong><code>PHP_URL_FRAGMENT</code></strong> to retrieve just a specific URL component as a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> (except when <strong><code>PHP_URL_PORT</code></strong> is given, in which case the return value will be an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>). </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.parse-url-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> On seriously malformed URLs, <span class="function"><strong>parse_url()</strong></span> may return <strong><code>FALSE</code></strong>. </p> <p class="para"> If the <em><code class="parameter">component</code></em> parameter is omitted, an associative <span class="type"><a href="language.types.array.html" class="type array">array</a></span> is returned. At least one element will be present within the array. Potential keys within this array are: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <var class="varname"><var class="varname">scheme</var></var> - e.g. 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> - after the question mark <em>?</em> </span> </li> <li class="listitem"> <span class="simpara"> <var class="varname"><var class="varname">fragment</var></var> - after the hashmark <em>#</em> </span> </li> </ul> </p> <p class="para"> If the <em><code class="parameter">component</code></em> parameter is specified, <span class="function"><strong>parse_url()</strong></span> returns a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> (or an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>, in the case of <strong><code>PHP_URL_PORT</code></strong>) instead of an <span class="type"><a href="language.types.array.html" class="type array">array</a></span>. If the requested component doesn't exist within the given URL, <strong><code>NULL</code></strong> will be returned. </p> </div> <div class="refsect1 changelog" id="refsect1-function.parse-url-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.4.7</td> <td> Fixed <em class="emphasis">host</em> recognition when <em class="emphasis">scheme</em> is omitted and a leading component separator is present. </td> </tr> <tr> <td>5.3.3</td> <td> Removed the <strong><code>E_WARNING</code></strong> that was emitted when URL parsing failed. </td> </tr> <tr> <td>5.1.2</td> <td>Added the <em><code class="parameter">component</code></em> parameter.</td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.parse-url-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4089"> <p><strong>Example #1 A <span class="function"><strong>parse_url()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><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> </code></div> </div> <div class="example-contents"><p>The above example will output:</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-4090"> <p><strong>Example #2 A <span class="function"><strong>parse_url()</strong></span> example with missing scheme</strong></p> <div class="example-contents"> <div class="phpcode"><code><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">// Prior to 5.4.7 this would show the path as "//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> </code></div> </div> <div class="example-contents"><p>The above example will output:</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">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This function doesn't work with relative URLs. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This function is intended specifically for the purpose of parsing URLs and not URIs. However, to comply with PHP's backwards compatibility requirements it makes an exception for the file:// scheme where triple slashes (file:///...) are allowed. For any other scheme this is invalid. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.parse-url-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.pathinfo.html" class="function" rel="rdfs-seeAlso">pathinfo()</a> - Returns information about a file path</span></li> <li class="member"> <span class="function"><a href="function.parse-str.html" class="function" rel="rdfs-seeAlso">parse_str()</a> - Parses the string into variables</span></li> <li class="member"> <span class="function"><a href="function.http-build-query.html" class="function" rel="rdfs-seeAlso">http_build_query()</a> - Generate URL-encoded query string</span></li> <li class="member"> <span class="function"><a href="function.http-build-url.html" class="function" rel="rdfs-seeAlso">http_build_url()</a> - Build a URL</span></li> <li class="member"> <span class="function"><a href="function.dirname.html" class="function" rel="rdfs-seeAlso">dirname()</a> - Returns parent directory's path</span></li> <li class="member"> <span class="function"><a href="function.basename.html" class="function" rel="rdfs-seeAlso">basename()</a> - Returns trailing name component of path</span></li> <li class="member"><a href="http://www.faqs.org/rfcs/rfc3986" class="link external">» RFC 3986</a></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" 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>