php-doc-en/reference/strings/functions/explode.xml
2011-08-26 08:05:42 +00:00

221 lines
5.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.explode">
<refnamediv>
<refname>explode</refname>
<refpurpose>Split a string by string</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>explode</methodname>
<methodparam><type>string</type><parameter>delimiter</parameter></methodparam>
<methodparam><type>string</type><parameter>string</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>limit</parameter></methodparam>
</methodsynopsis>
<para>
Returns an array of strings, each of which is a substring of
<parameter>string</parameter> formed by splitting it on
boundaries formed by the string <parameter>delimiter</parameter>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>delimiter</parameter></term>
<listitem>
<para>
The boundary string.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>string</parameter></term>
<listitem>
<para>
The input string.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>limit</parameter></term>
<listitem>
<para>
If <parameter>limit</parameter> is set and positive, the returned array will contain
a maximum of <parameter>limit</parameter> elements with the last
element containing the rest of <parameter>string</parameter>.
</para>
<para>
If the <parameter>limit</parameter> parameter is negative, all components
except the last -<parameter>limit</parameter> are returned.
</para>
<para>
If the <parameter>limit</parameter> parameter is zero, then this is treated as 1.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
Although <function>implode</function> can, for historical reasons,
accept its parameters in either order,
<function>explode</function> cannot. You must ensure that the
<parameter>delimiter</parameter> argument comes before the
<parameter>string</parameter> argument.
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns an <type>array</type> of <type>string</type>s
created by splitting the <parameter>string</parameter> parameter on
boundaries formed by the <parameter>delimiter</parameter>.
</para>
<para>
If <parameter>delimiter</parameter> is an empty <type>string</type> (""),
<function>explode</function> will return &false;.
If <parameter>delimiter</parameter> contains a value that is not
contained in <parameter>string</parameter> and a negative
<parameter>limit</parameter> is used, then an empty <type>array</type> will be
returned, otherwise an <type>array</type> containing
<parameter>string</parameter> will be returned.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>5.1.0</entry>
<entry>
Support for negative <parameter>limit</parameter>s was added
</entry>
</row>
<row>
<entry>4.0.1</entry>
<entry>
The <parameter>limit</parameter> parameter was added
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>explode</function> examples</title>
<programlisting role="php">
<![CDATA[
<?php
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title><parameter>limit</parameter> parameter examples</title>
<programlisting role="php">
<![CDATA[
<?php
$str = 'one|two|three|four';
// positive limit
print_r(explode('|', $str, 2));
// negative limit (since PHP 5.1)
print_r(explode('|', $str, -1));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
&note.bin-safe;
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>preg_split</function></member>
<member><function>str_split</function></member>
<member><function>mb_split</function></member>
<member><function>str_word_count</function></member>
<member><function>strtok</function></member>
<member><function>implode</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
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
-->