php-doc-en/reference/strings/functions/explode.xml

143 lines
3.8 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.8 $ -->
<!-- splitted from ./en/functions/strings.xml, last change in rev 1.2 -->
<refentry id="function.explode">
<refnamediv>
<refname>explode</refname>
<refpurpose>Split a string by string</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>explode</methodname>
<methodparam><type>string</type><parameter>separator</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>separator</parameter>.
If <parameter>limit</parameter> is set, 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 <parameter>separator</parameter> is an empty string (""),
<function>explode</function> will return &false;. If
<parameter>separator</parameter> contains a value that is not contained
in <parameter>string</parameter>, then <function>explode</function> will
return an array containing <parameter>string</parameter>.
</para>
<para>
If the <parameter>limit</parameter> parameter is negative, all components
except the last <parameter>limit</parameter> are returned. This feature
was added in PHP 5.1.0.
</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>separator</parameter> argument comes before the
<parameter>string</parameter> argument.
</para>
<note>
<para>
The <parameter>limit</parameter> parameter was added in PHP
4.0.1
</para>
</note>
<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
print_r(explode('|', $str, -1));
?>
]]>
</programlisting>
<para>
The above example will output:
</para>
<screen>
<![CDATA[
Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)
]]>
</screen>
</example>
</para>
&note.bin-safe;
<para>
See also
<function>preg_split</function>,
<function>spliti</function>,
<function>split</function>, and
<function>implode</function>.
</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:"../../../../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
-->