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.
|
2017-09-12 10:58:12 +00:00
|
|
|
The values of the array elements are <emphasis>not</emphasis> URL decoded.
|
2005-10-11 21:39:43 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
This function is <emphasis role="strong">not</emphasis> meant to validate
|
2021-06-16 16:08:30 +00:00
|
|
|
the given URL, it only breaks it up into the above listed parts. Partial and invalid
|
2005-10-11 21:39:43 +00:00
|
|
|
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>
|
2021-09-24 16:16:24 +00:00
|
|
|
The URL to parse.
|
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
|
2020-11-02 15:39:04 +00:00
|
|
|
value will be an <type>int</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
|
2020-11-02 15:39:04 +00:00
|
|
|
<type>int</type>, in the case of <constant>PHP_URL_PORT</constant>)
|
2011-02-04 11:11:05 +00:00
|
|
|
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>
|
2021-09-24 15:56:13 +00:00
|
|
|
<para>
|
|
|
|
Note that control characters (cf. <function>ctype_cntrl</function>) in the
|
|
|
|
components are replaced with underscores (<literal>_</literal>).
|
|
|
|
</para>
|
2006-08-12 18:00:17 +00:00
|
|
|
</refsect1>
|
|
|
|
|
2005-10-11 21:39:43 +00:00
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>A <function>parse_url</function> example</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2015-04-13 14:21:16 +00:00
|
|
|
$url = 'http://username:password@hostname:9090/path?arg=value#anchor';
|
2005-10-11 21:39:43 +00:00
|
|
|
|
2015-04-13 14:21:16 +00:00
|
|
|
var_dump(parse_url($url));
|
|
|
|
var_dump(parse_url($url, PHP_URL_SCHEME));
|
|
|
|
var_dump(parse_url($url, PHP_URL_USER));
|
|
|
|
var_dump(parse_url($url, PHP_URL_PASS));
|
|
|
|
var_dump(parse_url($url, PHP_URL_HOST));
|
|
|
|
var_dump(parse_url($url, PHP_URL_PORT));
|
|
|
|
var_dump(parse_url($url, PHP_URL_PATH));
|
|
|
|
var_dump(parse_url($url, PHP_URL_QUERY));
|
|
|
|
var_dump(parse_url($url, PHP_URL_FRAGMENT));
|
2005-10-11 21:39:43 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
2015-04-13 14:21:16 +00:00
|
|
|
array(8) {
|
|
|
|
["scheme"]=>
|
|
|
|
string(4) "http"
|
|
|
|
["host"]=>
|
|
|
|
string(8) "hostname"
|
|
|
|
["port"]=>
|
|
|
|
int(9090)
|
|
|
|
["user"]=>
|
|
|
|
string(8) "username"
|
|
|
|
["pass"]=>
|
|
|
|
string(8) "password"
|
|
|
|
["path"]=>
|
|
|
|
string(5) "/path"
|
|
|
|
["query"]=>
|
|
|
|
string(9) "arg=value"
|
|
|
|
["fragment"]=>
|
|
|
|
string(6) "anchor"
|
|
|
|
}
|
|
|
|
string(4) "http"
|
|
|
|
string(8) "username"
|
|
|
|
string(8) "password"
|
|
|
|
string(8) "hostname"
|
|
|
|
int(9090)
|
|
|
|
string(5) "/path"
|
|
|
|
string(9) "arg=value"
|
|
|
|
string(6) "anchor"
|
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;
|
2013-07-24 19:30:05 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2017-09-12 11:09:52 +00:00
|
|
|
This function may not give correct results for relative URLs.
|
2013-07-24 19:30:05 +00:00
|
|
|
</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>
|
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
|
|
|
|
-->
|