reference/pcre/functions/preg-match.xml
41c8533ff5a8f93ba9fdf0732d05dd5ab79864c9
...
...
@@ -9,10 +9,10 @@
9
9
<refsect1 role="description">
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
-
<type>int</type><methodname>preg_match</methodname>
12
+
<type class="union"><type>int</type><type>false</type></type><methodname>preg_match</methodname>
13
13
<methodparam><type>string</type><parameter>pattern</parameter></methodparam>
14
14
<methodparam><type>string</type><parameter>subject</parameter></methodparam>
15
-
<methodparam choice="opt"><type>array</type><parameter role="reference">matches</parameter></methodparam>
15
+
<methodparam choice="opt"><type>array</type><parameter role="reference">matches</parameter><initializer>&null;</initializer></methodparam>
16
16
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
17
17
<methodparam choice="opt"><type>int</type><parameter>offset</parameter><initializer>0</initializer></methodparam>
18
18
</methodsynopsis>
...
...
@@ -58,14 +58,14 @@
58
58
<term><parameter>flags</parameter></term>
59
59
<listitem>
60
60
<para>
61
-
<parameter>flags</parameter> can be the following flag:
61
+
<parameter>flags</parameter> can be a combination of the following flags:
62
62
<variablelist>
63
63
<varlistentry>
64
64
<term><constant>PREG_OFFSET_CAPTURE</constant></term>
65
65
<listitem>
66
66
<para>
67
67
If this flag is passed, for every occurring match the appendant string
68
-
offset will also be returned. Note that this changes the value of
68
+
offset (in bytes) will also be returned. Note that this changes the value of
69
69
<parameter>matches</parameter> into an array where every element is an
70
70
array consisting of the matched string at offset <literal>0</literal>
71
71
and its string offset into <parameter>subject</parameter> at offset
...
...
@@ -115,6 +115,52 @@ Array
115
115
</para>
116
116
</listitem>
117
117
</varlistentry>
118
+
<varlistentry>
119
+
<term><constant>PREG_UNMATCHED_AS_NULL</constant></term>
120
+
<listitem>
121
+
<para>
122
+
If this flag is passed, unmatched subpatterns are reported as &null;;
123
+
otherwise they are reported as an empty <type>string</type>.
124
+
<informalexample>
125
+
<programlisting role="php">
126
+
<![CDATA[
127
+
<?php
128
+
preg_match('/(a)(b)*(c)/', 'ac', $matches);
129
+
var_dump($matches);
130
+
preg_match('/(a)(b)*(c)/', 'ac', $matches, PREG_UNMATCHED_AS_NULL);
131
+
var_dump($matches);
132
+
?>
133
+
]]>
134
+
</programlisting>
135
+
&example.outputs;
136
+
<screen>
137
+
<![CDATA[
138
+
array(4) {
139
+
[0]=>
140
+
string(2) "ac"
141
+
[1]=>
142
+
string(1) "a"
143
+
[2]=>
144
+
string(0) ""
145
+
[3]=>
146
+
string(1) "c"
147
+
}
148
+
array(4) {
149
+
[0]=>
150
+
string(2) "ac"
151
+
[1]=>
152
+
string(1) "a"
153
+
[2]=>
154
+
NULL
155
+
[3]=>
156
+
string(1) "c"
157
+
}
158
+
]]>
159
+
</screen>
160
+
</informalexample>
161
+
</para>
162
+
</listitem>
163
+
</varlistentry>
118
164
</variablelist>
119
165
</para>
120
166
</listitem>
...
...
@@ -130,7 +176,7 @@ Array
130
176
<note>
131
177
<para>
132
178
Using <parameter>offset</parameter> is not equivalent to passing
133
-
<literal>substr($subject, $offset)</literal> to
179
+
<code>substr($subject, $offset)</code> to
134
180
<function>preg_match</function> in place of the subject string,
135
181
because <parameter>pattern</parameter> can contain assertions such as
136
182
<emphasis>^</emphasis>, <emphasis>$</emphasis> or
...
...
@@ -183,6 +229,12 @@ Array
183
229
)
184
230
]]>
185
231
</screen>
232
+
<para>
233
+
Alternatively, to avoid using <function>substr</function>, use the
234
+
<literal>\G</literal> assertion rather than the <literal>^</literal> anchor, or
235
+
the <literal>A</literal> modifier instead, both of which work with
236
+
the <parameter>offset</parameter> parameter.
237
+
</para>
186
238
</informalexample>
187
239
</para>
188
240
</note>
...
...
@@ -196,12 +248,16 @@ Array
196
248
&reftitle.returnvalues;
197
249
<para>
198
250
<function>preg_match</function> returns 1 if the <parameter>pattern</parameter>
199
-
matches given <parameter>subject</parameter>, 0 if it does not, or &false;
200
-
if an error occurred.
251
+
matches given <parameter>subject</parameter>, 0 if it does not,&return.falseforfailure;.
201
252
</para>
202
253
&return.falseproblem;
203
254
</refsect1>
204
255

256
+
<refsect1 role="errors">
257
+
&reftitle.errors;
258
+
&pcre.pattern.warning;
259
+
</refsect1>
260
+

205
261
<refsect1 role="changelog">
206
262
&reftitle.changelog;
207
263
<para>
...
...
@@ -215,20 +271,10 @@ Array
215
271
</thead>
216
272
<tbody>
217
273
<row>
218
-
<entry>5.3.6</entry>
274
+
<entry>7.2.0</entry>
219
275
<entry>
220
-
Returns &false; if <parameter>offset</parameter> is higher than
221
-
<parameter>subject</parameter> length.
222
-
</entry>
223
-
</row>
224
-
<row>
225
-
<entry>5.2.2</entry>
226
-
<entry>
227
-
Named subpatterns now accept the
228
-
syntax <literal>(?&lt;name&gt;)</literal>
229
-
and <literal>(?'name')</literal> as well
230
-
as <literal>(?P&lt;name&gt;)</literal>. Previous versions
231
-
accepted only <literal>(?P&lt;name&gt;)</literal>.
276
+
The <constant>PREG_UNMATCHED_AS_NULL</constant> is now supported for the
277
+
<parameter>$flags</parameter> parameter.
232
278
</entry>
233
279
</row>
234
280
</tbody>
...
...
@@ -316,8 +362,7 @@ $str = 'foobar: 2008';
316
362

317
363
preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
318
364

319
-
/* This also works in PHP 5.2.2 (PCRE 7.0) and later, however
320
-
* the above form is recommended for backwards compatibility */
365
+
/* Alternative */
321
366
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
322
367

323
368
print_r($matches);
...
...
@@ -363,12 +408,12 @@ Array
363
408
<member><function>preg_replace</function></member>
364
409
<member><function>preg_split</function></member>
365
410
<member><function>preg_last_error</function></member>
411
+
<member><function>preg_last_error_msg</function></member>
366
412
</simplelist>
367
413
</para>
368
414
</refsect1>
369
415

370
416
</refentry>
371
-

372
417
<!-- Keep this comment at the end of the file
373
418
Local variables:
374
419
mode: sgml
375
420