php-doc-en/reference/pcre/functions/preg-split.xml
Juliette 41c8533ff5
PCRE: add missing "Errors/Exceptions" section
All PCRE pattern matching functions will throw a warning when an invalid regex is passed to the `$pattern` parameter.
See: https://3v4l.org/WkVIW

This was thus far undocumented behaviour.

This PR adds:
* The "Errors/Exceptions" section to each of the PCRE pattern matching function documentation pages (if it didn't exist yet).
* Adds a macro for the text snippet about the warning to display in the section and uses that in the "Errors/Exceptions" section for each function.

Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>

Closes GH-1057.
2021-11-08 13:01:56 +01:00

261 lines
6.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.preg-split" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>preg_split</refname>
<refpurpose>Split string by a regular expression</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type class="union"><type>array</type><type>false</type></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><initializer>-1</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
</methodsynopsis>
<para>
Split the given string by a regular expression.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>pattern</parameter></term>
<listitem>
<para>
The pattern to search for, as a string.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>subject</parameter></term>
<listitem>
<para>
The input string.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>limit</parameter></term>
<listitem>
<para>
If specified, then only substrings up to <parameter>limit</parameter>
are returned with the rest of the string being placed in the last
substring. A <parameter>limit</parameter> of -1 or 0 means "no limit".
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
<parameter>flags</parameter> can be any combination of the following
flags (combined with the <literal>|</literal> bitwise operator):
<variablelist>
<varlistentry>
<term><constant>PREG_SPLIT_NO_EMPTY</constant></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><constant>PREG_SPLIT_DELIM_CAPTURE</constant></term>
<listitem>
<simpara>
If this flag is set, parenthesized expression in the delimiter pattern
will be captured and returned as well.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>PREG_SPLIT_OFFSET_CAPTURE</constant></term>
<listitem>
<para>
If this flag is set, for every occurring 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 its string offset
into <parameter>subject</parameter> at offset <literal>1</literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns an array containing substrings of <parameter>subject</parameter>
split along boundaries matched by <parameter>pattern</parameter>, &return.falseforfailure;.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
&pcre.pattern.warning;
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>preg_split</function> example : Get the parts of a search string</title>
<programlisting role="php">
<![CDATA[
<?php
// 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");
print_r($keywords);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => hypertext
[1] => language
[2] => programming
)
]]>
</screen>
</example>
</para>
<para>
<example>
<title>Splitting a string into component characters</title>
<programlisting role="php">
<![CDATA[
<?php
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => s
[1] => t
[2] => r
[3] => i
[4] => n
[5] => g
)
]]>
</screen>
</example>
</para>
<para>
<example>
<title>Splitting a string into matches and their offsets</title>
<programlisting role="php">
<![CDATA[
<?php
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => Array
(
[0] => hypertext
[1] => 0
)
[1] => Array
(
[0] => language
[1] => 10
)
[2] => Array
(
[0] => programming
[1] => 19
)
)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<tip>
<para>
If you don't need the power of regular expressions, you can choose
faster (albeit simpler) alternatives like <function>explode</function>
or <function>str_split</function>.
</para>
</tip>
<tip>
<para>
If matching fails, an array with a single element containing the input string will be returned.
</para>
</tip>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="pcre.pattern">PCRE Patterns</link></member>
<member><function>preg_quote</function></member>
<member><function>implode</function></member>
<member><function>preg_match</function></member>
<member><function>preg_match_all</function></member>
<member><function>preg_replace</function></member>
<member><function>preg_last_error</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
-->