2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 09:15:58 +00:00
|
|
|
<!-- $Revision$ -->
|
2012-01-24 23:32:23 +00:00
|
|
|
<refentry xml:id="function.parse-url" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
2005-10-11 21:39:43 +00:00
|
|
|
<refnamediv>
|
|
|
|
<refname>parse_url</refname>
|
|
|
|
<refpurpose>Parse a URL and return its components</refpurpose>
|
|
|
|
</refnamediv>
|
2007-02-03 01:10:52 +00:00
|
|
|
|
2005-10-11 21:39:43 +00:00
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
|
|
|
<methodsynopsis>
|
2006-08-12 18:00:17 +00:00
|
|
|
<type>mixed</type><methodname>parse_url</methodname>
|
2005-10-11 21:39:43 +00:00
|
|
|
<methodparam><type>string</type><parameter>url</parameter></methodparam>
|
2009-01-16 16:14:26 +00:00
|
|
|
<methodparam choice="opt"><type>int</type><parameter>component</parameter><initializer>-1</initializer></methodparam>
|
2005-10-11 21:39:43 +00:00
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
|
|
|
This function parses a URL and returns an associative array containing any
|
|
|
|
of the various components of the URL that are present.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
This function is <emphasis role="strong">not</emphasis> meant to validate
|
|
|
|
the given URL, it only breaks it up into the above listed parts. Partial
|
|
|
|
URLs are also accepted, <function>parse_url</function> tries its best to
|
|
|
|
parse them correctly.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
|
|
|
<para>
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>url</parameter></term>
|
2002-04-15 00:12:54 +00:00
|
|
|
<listitem>
|
2005-10-11 21:39:43 +00:00
|
|
|
<para>
|
2008-11-07 12:31:32 +00:00
|
|
|
The URL to parse. Invalid characters are replaced by
|
|
|
|
<literal>_</literal>.
|
2005-10-11 21:39:43 +00:00
|
|
|
</para>
|
2002-04-15 00:12:54 +00:00
|
|
|
</listitem>
|
2005-10-11 21:39:43 +00:00
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
2006-08-12 18:00:17 +00:00
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>component</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Specify one of <constant>PHP_URL_SCHEME</constant>,
|
|
|
|
<constant>PHP_URL_HOST</constant>, <constant>PHP_URL_PORT</constant>,
|
|
|
|
<constant>PHP_URL_USER</constant>, <constant>PHP_URL_PASS</constant>,
|
|
|
|
<constant>PHP_URL_PATH</constant>, <constant>PHP_URL_QUERY</constant>
|
|
|
|
or <constant>PHP_URL_FRAGMENT</constant> to retrieve just a specific
|
2011-02-04 11:11:05 +00:00
|
|
|
URL component as a <type>string</type> (except when
|
|
|
|
<constant>PHP_URL_PORT</constant> is given, in which case the return
|
|
|
|
value will be an <type>integer</type>).
|
2006-08-12 18:00:17 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
2005-10-11 21:39:43 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
|
|
|
<para>
|
|
|
|
On seriously malformed URLs, <function>parse_url</function> may return
|
2010-11-23 11:07:51 +00:00
|
|
|
&false;.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
If the <parameter>component</parameter> parameter is omitted, an
|
|
|
|
associative <type>array</type> is returned. At least one element will be
|
|
|
|
present within the array. Potential keys within this array are:
|
2005-10-11 21:39:43 +00:00
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2007-06-20 22:25:43 +00:00
|
|
|
<varname remap="structfield">scheme</varname> - e.g. http
|
2005-10-11 21:39:43 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2007-06-20 22:25:43 +00:00
|
|
|
<varname remap="structfield">host</varname>
|
2005-10-11 21:39:43 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2007-06-20 22:25:43 +00:00
|
|
|
<varname remap="structfield">port</varname>
|
2005-10-11 21:39:43 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2007-06-20 22:25:43 +00:00
|
|
|
<varname remap="structfield">user</varname>
|
2005-10-11 21:39:43 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2007-06-20 22:25:43 +00:00
|
|
|
<varname remap="structfield">pass</varname>
|
2005-10-11 21:39:43 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2007-06-20 22:25:43 +00:00
|
|
|
<varname remap="structfield">path</varname>
|
2005-10-11 21:39:43 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2007-06-20 22:25:43 +00:00
|
|
|
<varname remap="structfield">query</varname> - after the question mark <literal>?</literal>
|
2005-10-11 21:39:43 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2007-06-20 22:25:43 +00:00
|
|
|
<varname remap="structfield">fragment</varname> - after the hashmark <literal>#</literal>
|
2005-10-11 21:39:43 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
2006-08-12 18:00:17 +00:00
|
|
|
<para>
|
2010-11-23 11:07:51 +00:00
|
|
|
If the <parameter>component</parameter> parameter is specified,
|
2011-02-04 11:11:05 +00:00
|
|
|
<function>parse_url</function> returns a <type>string</type> (or an
|
|
|
|
<type>integer</type>, in the case of <constant>PHP_URL_PORT</constant>)
|
|
|
|
instead of an <type>array</type>. If the requested component doesn't exist
|
|
|
|
within the given URL, &null; will be returned.
|
2006-08-12 18:00:17 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="changelog">
|
|
|
|
&reftitle.changelog;
|
|
|
|
<para>
|
|
|
|
<informaltable>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>&Version;</entry>
|
|
|
|
<entry>&Description;</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2012-09-14 01:39:08 +00:00
|
|
|
<row>
|
|
|
|
<entry>5.4.7</entry>
|
|
|
|
<entry>
|
|
|
|
Fixed <emphasis>host</emphasis> recognition when <emphasis>scheme</emphasis>
|
2013-03-22 17:54:38 +00:00
|
|
|
is omitted and a leading component separator is present.
|
2012-09-14 01:39:08 +00:00
|
|
|
</entry>
|
|
|
|
</row>
|
2010-08-20 05:34:58 +00:00
|
|
|
<row>
|
|
|
|
<entry>5.3.3</entry>
|
|
|
|
<entry>
|
|
|
|
Removed the <constant>E_WARNING</constant> that was emitted when URL
|
|
|
|
parsing failed.
|
|
|
|
</entry>
|
|
|
|
</row>
|
2006-08-12 18:00:17 +00:00
|
|
|
<row>
|
|
|
|
<entry>5.1.2</entry>
|
2010-08-20 05:34:58 +00:00
|
|
|
<entry>Added the <parameter>component</parameter> parameter.</entry>
|
2006-08-12 18:00:17 +00:00
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</informaltable>
|
|
|
|
</para>
|
2005-10-11 21:39:43 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>A <function>parse_url</function> example</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
$url = 'http://username:password@hostname/path?arg=value#anchor';
|
|
|
|
|
|
|
|
print_r(parse_url($url));
|
2006-08-12 18:00:17 +00:00
|
|
|
|
|
|
|
echo parse_url($url, PHP_URL_PATH);
|
2005-10-11 21:39:43 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
2002-06-09 23:37:41 +00:00
|
|
|
Array
|
|
|
|
(
|
|
|
|
[scheme] => http
|
|
|
|
[host] => hostname
|
|
|
|
[user] => username
|
|
|
|
[pass] => password
|
|
|
|
[path] => /path
|
|
|
|
[query] => arg=value
|
|
|
|
[fragment] => anchor
|
|
|
|
)
|
2006-08-12 18:00:17 +00:00
|
|
|
/path
|
2012-09-14 01:39:08 +00:00
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>A <function>parse_url</function> example with missing scheme</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
$url = '//www.example.com/path?googleguy=googley';
|
|
|
|
|
|
|
|
// Prior to 5.4.7 this would show the path as "//www.example.com/path"
|
|
|
|
var_dump(parse_url($url));
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
array(3) {
|
|
|
|
["host"]=>
|
|
|
|
string(15) "www.example.com"
|
|
|
|
["path"]=>
|
|
|
|
string(5) "/path"
|
|
|
|
["query"]=>
|
|
|
|
string(17) "googleguy=googley"
|
|
|
|
}
|
2005-10-11 21:39:43 +00:00
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
2002-06-09 23:37:41 +00:00
|
|
|
|
2005-10-11 21:39:43 +00:00
|
|
|
<refsect1 role="notes">
|
|
|
|
&reftitle.notes;
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
This function doesn't work with relative URLs.
|
|
|
|
</para>
|
|
|
|
</note>
|
2005-10-27 21:20:26 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2007-02-03 01:10:52 +00:00
|
|
|
This function is intended specifically for the purpose of parsing URLs
|
|
|
|
and not URIs. However, to comply with PHP's backwards compatibility
|
2008-12-16 05:56:46 +00:00
|
|
|
requirements it makes an exception for the file:// scheme where triple
|
2007-02-03 01:10:52 +00:00
|
|
|
slashes (file:///...) are allowed. For any other scheme this is invalid.
|
2005-10-27 21:20:26 +00:00
|
|
|
</para>
|
2005-10-28 18:12:19 +00:00
|
|
|
</note>
|
2005-10-11 21:39:43 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
|
|
|
<para>
|
|
|
|
<simplelist>
|
|
|
|
<member><function>pathinfo</function></member>
|
|
|
|
<member><function>parse_str</function></member>
|
2006-08-12 18:00:17 +00:00
|
|
|
<member><function>http_build_query</function></member>
|
2009-11-13 19:55:01 +00:00
|
|
|
<member><function>http_build_url</function></member>
|
2005-10-11 21:39:43 +00:00
|
|
|
<member><function>dirname</function></member>
|
|
|
|
<member><function>basename</function></member>
|
2012-01-24 23:32:23 +00:00
|
|
|
<member><link xlink:href="&url.rfc;3986">RFC 3986</link></member>
|
2005-10-11 21:39:43 +00:00
|
|
|
</simplelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2002-04-15 00:12:54 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
|
|
|
-->
|