1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.11 $ -->
<!-- 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>
<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><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. This flag was added for 4.0.5.
</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>.
This flag is available since PHP 4.3.0 .
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<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>
<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");
?>
]]>
</programlisting>
</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>
</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>
<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>
<note>
<para>
Parameter <parameter>flags</parameter> was added in PHP 4 Beta 3.
</para>
</note>
<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
-->
|