php-doc-en/reference/pcre/functions/preg-split.xml
Markus Fischer 0f428eadd3 - Update to latest PCRE changes.
# Would be nice if someone can provide examples for preg_match() and
# preg_match_all() with the new PREG_OFFSET_CAPTURE flag.


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@85550 c90b9560-bf6c-de11-be94-00142212c4b1
2002-06-13 07:55:02 +00:00

171 lines
4.8 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- splitted from ./en/functions/pcre.xml, last change in rev 1.2 -->
<refentry id="function.preg-split">
<refnamediv>
<refname>preg_split</refname>
<refpurpose>Split string by a regular expression</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>preg_split</methodname>
<methodparam><type>string</type><parameter>pattern</parameter></methodparam>
<methodparam><type>string</type><parameter>subject</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>limit</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>flags</parameter></methodparam>
</methodsynopsis>
<note>
<para>
Parameter <parameter>flags</parameter> was added in PHP 4 Beta 3.
</para>
</note>
<para>
Returns an array containing substrings of
<parameter>subject</parameter> split along boundaries matched by
<parameter>pattern</parameter>.
</para>
<para>
If <parameter>limit</parameter> is specified, then only substrings up to
<parameter>limit</parameter> are returned, and if
<parameter>limit</parameter> is -1, it actually means "no limit", which is
useful for specifying the <parameter>flags</parameter>.
</para>
<para>
<parameter>flags</parameter> can be any combination of the following flags
(combined with bitwise | operator):
<variablelist>
<varlistentry>
<term>PREG_SPLIT_NO_EMPTY</term>
<listitem>
<simpara>
If this flag is set, only non-empty pieces will be returned by
<function>preg_split</function>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>PREG_SPLIT_DELIM_CAPTURE</term>
<listitem>
<simpara>
If this flag is set, parenthesized expression in the delimiter pattern
will be captured and returned as well. This flag was added for 4.0.5.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>PREG_SPLIT_OFFSET_CAPTURE</term>
<listitem>
<para>
If this flag is set, for every occuring match the appendant string
offset will also be returned. Note that this changes the return
value in an array where every element is an array consisting of the
matched string at offset <literal>0</literal> and it's string offset
into <parameter>subject</parameter> at offset <literal>1</literal>.
This flag is available since <literal>PHP</literal> 4.3.0 .
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
<example>
<title><function>preg_split</function> example : Get the parts of a search string.</title>
<programlisting role="php">
<![CDATA[
// split the phrase by any number of commas or space characters,
// which include " ", \r, \t, \n and \f
$keywords = preg_split ("/[\s,]+/", "hypertext language, programming");
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Splitting a string into component characters.</title>
<programlisting role="php">
<![CDATA[
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Splitting a string into matches and their offsets.</title>
<programlisting role="php">
<![CDATA[
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
]]>
</programlisting>
<para>
will yield
</para>
<screen>
<![CDATA[
Array
(
[0] => Array
(
[0] => hypertext
[1] => 0
)
[1] => Array
(
[0] => language
[1] => 10
)
[2] => Array
(
[0] => programming
[1] => 19
)
)
]]>
</screen>
</example>
</para>
<para>
See also
<function>spliti</function>,
<function>split</function>,
<function>implode</function>,
<function>preg_match</function>,
<function>preg_match_all</function>, and
<function>preg_replace</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
-->