ref.pcre: switch to new doc style

# And fix #40215


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@228785 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Mehdi Achour 2007-02-02 22:11:46 +00:00
parent 64a1c7cc9e
commit 039d648a12
8 changed files with 773 additions and 324 deletions

View file

@ -1,49 +1,108 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.7 $ -->
<!-- $Revision: 1.8 $ -->
<refentry id="function.preg-grep">
<refnamediv>
<refname>preg_grep</refname>
<refpurpose>Return array entries that match the pattern</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>preg_grep</methodname>
<methodparam><type>string</type><parameter>pattern</parameter></methodparam>
<methodparam><type>array</type><parameter>input</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>flags</parameter></methodparam>
</methodsynopsis>
<para>
<function>preg_grep</function> returns the array consisting of
the elements of the <parameter>input</parameter> array that match
the given <parameter>pattern</parameter>.
Returns the array consisting of the elements of the
<parameter>input</parameter> array that match the given
<parameter>pattern</parameter>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<parameter>flags</parameter> can be the following flag:
<variablelist>
<varlistentry>
<term>PREG_GREP_INVERT</term>
<term><parameter>pattern</parameter></term>
<listitem>
<simpara>
If this flag is passed, <function>preg_grep</function> returns the
elements of the input array that do <emphasis>not</emphasis> match
<para>
The pattern to search for, as a string.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>input</parameter></term>
<listitem>
<para>
The input array.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
If set to <constant>PREG_GREP_INVERT</constant>, this function returns
the elements of the input array that do <emphasis>not</emphasis> match
the given <parameter>pattern</parameter>.
This flag is available since PHP 4.2.0.
</simpara>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Since PHP 4.0.4, the results returned by <function>preg_grep</function>
are indexed using the keys from the input array. If this behavior is
undesirable, use <function>array_values</function> on the array returned by
<function>preg_grep</function> to reindex the values.
Returns an array indexed using the keys from the
<parameter>input</parameter> array.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>4.2.0</entry>
<entry>
The <parameter>flags</parameter> parameter was added.
</entry>
</row>
<row>
<entry>4.0.4</entry>
<entry>
<para>
Prior to this version, the returned array was indexed regardless of
the keys of the <parameter>input</parameter> array.
</para>
<para>
If you want to reproduce this old behavior, use
<function>array_values</function> on the returned array to reindex
the values.
</para>
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>preg_grep</function> example</title>
@ -59,6 +118,7 @@ $fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.3 $ -->
<!-- $Revision: 1.4 $ -->
<refentry id="function.preg-last-error">
<refnamediv>
<refname>preg_last_error</refname>
@ -33,6 +33,7 @@
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -1,12 +1,13 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.21 $ -->
<!-- $Revision: 1.22 $ -->
<refentry id="function.preg-match-all">
<refnamediv>
<refname>preg_match_all</refname>
<refpurpose>Perform a global regular expression match</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>preg_match_all</methodname>
<methodparam><type>string</type><parameter>pattern</parameter></methodparam>
@ -25,23 +26,59 @@
After the first match is found, the subsequent searches are continued
on from end of the last match.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<parameter>flags</parameter> can be a combination of the following flags
(note that it doesn't make sense to use
<constant>PREG_PATTERN_ORDER</constant> together with
<constant>PREG_SET_ORDER</constant>):
<variablelist>
<varlistentry>
<term>PREG_PATTERN_ORDER</term>
<term><parameter>pattern</parameter></term>
<listitem>
<para>
Orders results so that $matches[0] is an array of full
pattern matches, $matches[1] is an array of strings matched by
the first parenthesized subpattern, and so on.
The pattern to search for, as a string.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>subject</parameter></term>
<listitem>
<para>
<informalexample>
<programlisting role="php">
The input string.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>matches</parameter></term>
<listitem>
<para>
In this case, $matches[0] is the first set of matches, and
<varname>$matches[0][0]</varname> has text matched by full pattern,
<varname>$matches[0][1]</varname> has text matched by first
subpattern and so on. Similarly, <varname>$matches[1]</varname> is
the second set of matches, etc.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
Can be a combination of the following flags (note that it doesn't make
sense to use <constant>PREG_PATTERN_ORDER</constant> together with
<constant>PREG_SET_ORDER</constant>):
<variablelist>
<varlistentry>
<term><constant>PREG_PATTERN_ORDER</constant></term>
<listitem>
<para>
Orders results so that $matches[0] is an array of full
pattern matches, $matches[1] is an array of strings matched by
the first parenthesized subpattern, and so on.
</para>
<para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
@ -51,33 +88,31 @@ echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
?>
]]>
</programlisting>
<para>
This example will produce:
</para>
<screen role="html">
</programlisting>
&example.outputs;
<screen role="html">
<![CDATA[
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test
]]>
</screen>
<para>
So, $out[0] contains array of strings that matched full pattern,
and $out[1] contains array of strings enclosed by tags.
</para>
</informalexample>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>PREG_SET_ORDER</term>
<listitem>
<para>
Orders results so that $matches[0] is an array of first set
of matches, $matches[1] is an array of second set of matches,
and so on.
<informalexample>
<programlisting role="php">
</screen>
<para>
So, $out[0] contains array of strings that matched full pattern,
and $out[1] contains array of strings enclosed by tags.
</para>
</informalexample>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>PREG_SET_ORDER</constant></term>
<listitem>
<para>
Orders results so that $matches[0] is an array of first set
of matches, $matches[1] is an array of second set of matches,
and so on.
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
@ -87,71 +122,104 @@ echo $out[0][0] . ", " . $out[0][1] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
?>
]]>
</programlisting>
<para>
This example will produce:
</para>
<screen role="html">
</programlisting>
&example.outputs;
<screen role="html">
<![CDATA[
<b>example: </b>, example:
<div align="left">this is a test</div>, this is a test
]]>
</screen>
</informalexample>
</screen>
</informalexample>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><constant>PREG_OFFSET_CAPTURE</constant></term>
<listitem>
<para>
If this flag is passed, for every occurring match the appendant string
offset will also be returned. Note that this changes the value of
<parameter>matches</parameter> 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>
<para>
In this case, $matches[0] is the first set of matches, and
$matches[0][0] has text matched by full pattern, $matches[0][1]
has text matched by first subpattern and so on. Similarly,
$matches[1] is the second set of matches, etc.
If no order flag is given, <constant>PREG_PATTERN_ORDER</constant> is
assumed.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>PREG_OFFSET_CAPTURE</term>
<term><parameter>offset</parameter></term>
<listitem>
<para>
If this flag is passed, for every occurring match the appendant string
offset will also be returned. Note that this changes the value of
<parameter>matches</parameter> 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 .
Normally, the search starts from the beginning of the subject string.
The optional parameter <parameter>offset</parameter> can be used to
specify the alternate place from which to start the search.
</para>
<note>
<para>
Using <parameter>offset</parameter> is not equivalent to passing
<literal>substr($subject, $offset)</literal> to
<function>preg_match_all</function> in place of the subject string,
because <parameter>pattern</parameter> can contain assertions such as
<emphasis>^</emphasis>, <emphasis>$</emphasis> or
<emphasis>(?&lt;=x)</emphasis>. See <function>preg_match</function>
for examples.
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>
If no order flag is given, <constant>PREG_PATTERN_ORDER</constant> is
assumed.
</para>
<para>
Normally, the search starts from the beginning of the subject string. The
optional parameter <parameter>offset</parameter> can be used to specify
the alternate place from which to start the search.
The <parameter>offset</parameter> parameter is available since
PHP 4.3.3.
</para>
<note>
<para>
Using <parameter>offset</parameter> is not equivalent to
passing <literal>substr($subject, $offset)</literal> to
<function>preg_match_all</function> in place of the subject string, because
<parameter>pattern</parameter> can contain assertions such as
<emphasis>^</emphasis>, <emphasis>$</emphasis> or
<emphasis>(?&lt;=x)</emphasis>. See <function>preg_match</function> for
examples.
</para>
</note>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the number of full pattern matches (which might be zero),
or &false; if an error occurred.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>4.3.3</entry>
<entry>
The <parameter>offset</parameter> parameter was added
</entry>
</row>
<row>
<entry>4.3.0</entry>
<entry>
The <constant>PREG_OFFSET_CAPTURE</constant> flag was added
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Getting all phone numbers out of some text.</title>
@ -188,9 +256,7 @@ foreach ($matches as $val) {
?>
]]>
</programlisting>
<para>
This example will produce:
</para>
&example.outputs;
<screen role="html">
<![CDATA[
matched: <b>bold text</b>
@ -206,12 +272,19 @@ part 3: </a>
</screen>
</example>
</para>
<simpara>
See also <function>preg_match</function>,
<function>preg_replace</function>,
and <function>preg_split</function>.
</simpara>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>preg_match</function></member>
<member><function>preg_replace</function></member>
<member><function>preg_split</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -1,12 +1,13 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.21 $ -->
<!-- $Revision: 1.22 $ -->
<refentry id="function.preg-match">
<refnamediv>
<refname>preg_match</refname>
<refpurpose>Perform a regular expression match</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>preg_match</methodname>
<methodparam><type>string</type><parameter>pattern</parameter></methodparam>
@ -19,52 +20,80 @@
Searches <parameter>subject</parameter> for a match to the regular
expression given in <parameter>pattern</parameter>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
If <parameter>matches</parameter> is provided, then it is filled with the
results of search. <varname>$matches[0]</varname> will contain the text
that matched the full pattern, <varname>$matches[1]</varname> will have
the text that matched the first captured parenthesized subpattern, and so
on.
</para>
<para>
<parameter>flags</parameter> can be the following flag:
<variablelist>
<varlistentry>
<term>PREG_OFFSET_CAPTURE</term>
<term><parameter>pattern</parameter></term>
<listitem>
<simpara>
If this flag is passed, 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 .
</simpara>
<para>
The pattern to search for, as a string.
</para>
</listitem>
</varlistentry>
</variablelist>
The <parameter>flags</parameter> parameter is available since
PHP 4.3.0.
</para>
<para>
Normally, the search starts from the beginning of the subject string. The
optional parameter <parameter>offset</parameter> can be used to specify
the alternate place from which to start the search.
The <parameter>offset</parameter> parameter is available since
PHP 4.3.3.
</para>
<note>
<para>
Using <parameter>offset</parameter> is not equivalent to
passing <literal>substr($subject, $offset)</literal> to
<function>preg_match</function> in place of the subject string, because
<parameter>pattern</parameter> can contain assertions such as
<emphasis>^</emphasis>, <emphasis>$</emphasis> or
<emphasis>(?&lt;=x)</emphasis>. Compare:
</para>
<informalexample>
<programlisting role="php">
<varlistentry>
<term><parameter>subject</parameter></term>
<listitem>
<para>
The input string.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>matches</parameter></term>
<listitem>
<para>
If <parameter>matches</parameter> is provided, then it is filled with
the results of search. <varname>$matches[0]</varname> will contain the
text that matched the full pattern, <varname>$matches[1]</varname>
will have the text that matched the first captured parenthesized
subpattern, and so on.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
<parameter>flags</parameter> can be the following flag:
<variablelist>
<varlistentry>
<term><constant>PREG_OFFSET_CAPTURE</constant></term>
<listitem>
<simpara>
If this flag is passed, 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>.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>offset</parameter></term>
<listitem>
<para>
Normally, the search starts from the beginning of the subject string.
The optional parameter <parameter>offset</parameter> can be used to
specify the alternate place from which to start the search.
</para>
<note>
<para>
Using <parameter>offset</parameter> is not equivalent to passing
<literal>substr($subject, $offset)</literal> to
<function>preg_match_all</function> in place of the subject string,
because <parameter>pattern</parameter> can contain assertions such as
<emphasis>^</emphasis>, <emphasis>$</emphasis> or
<emphasis>(?&lt;=x)</emphasis>. Compare:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$subject = "abcdef";
@ -73,19 +102,19 @@ preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
]]>
</programlisting>
&example.outputs;
<screen>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
)
]]>
</screen>
<para>
while this example
</para>
<programlisting role="php">
</screen>
<para>
while this example
</para>
<programlisting role="php">
<![CDATA[
<?php
$subject = "abcdef";
@ -94,11 +123,11 @@ preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
]]>
</programlisting>
<para>
will produce
</para>
<screen>
</programlisting>
<para>
will produce
</para>
<screen>
<![CDATA[
Array
(
@ -110,10 +139,18 @@ Array
)
]]>
</screen>
</informalexample>
</note>
</screen>
</informalexample>
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<function>preg_match</function> returns the number of times
<parameter>pattern</parameter> matches. That will be either 0 times
@ -123,14 +160,46 @@ Array
<parameter>subject</parameter>.
<function>preg_match</function> returns &false; if an error occurred.
</para>
<tip>
<para>
Do not use <function>preg_match</function> if you only want to check if
one string is contained in another string. Use
<function>strpos</function> or <function>strstr</function> instead as
they will be faster.
</para>
</tip>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>4.3.3</entry>
<entry>
The <parameter>offset</parameter> parameter was added
</entry>
</row>
<row>
<entry>4.3.0</entry>
<entry>
The <constant>PREG_OFFSET_CAPTURE</constant> flag was added
</entry>
</row>
<row>
<entry>4.3.0</entry>
<entry>
The <parameter>flags</parameter> parameter was added
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Find the string of text "php"</title>
@ -197,12 +266,31 @@ domain name is: php.net
</screen>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<tip>
<para>
Do not use <function>preg_match</function> if you only want to check if
one string is contained in another string. Use
<function>strpos</function> or <function>strstr</function> instead as
they will be faster.
</para>
</tip>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
See also <function>preg_match_all</function>,
<function>preg_replace</function>, and
<function>preg_split</function>.
<simplelist>
<member><function>preg_match_all</function></member>
<member><function>preg_replace</function></member>
<member><function>preg_split</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -1,12 +1,13 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.9 $ -->
<!-- $Revision: 1.10 $ -->
<refentry id="function.preg-quote">
<refnamediv>
<refname>preg_quote</refname>
<refpurpose>Quote regular expression characters</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>string</type><methodname>preg_quote</methodname>
<methodparam><type>string</type><parameter>str</parameter></methodparam>
@ -19,15 +20,48 @@
run-time string that you need to match in some text and the
string may contain special regex characters.
</para>
<para>
If the optional <parameter>delimiter</parameter> is specified, it
will also be escaped. This is useful for escaping the delimiter
that is required by the PCRE functions. The / is the most commonly
used delimiter.</para>
<para>
The special regular expression characters are:
<literal>. \ + * ? [ ^ ] $ ( ) { } = ! &lt; &gt; | :</literal>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>str</parameter></term>
<listitem>
<para>
The input string.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>delimiter</parameter></term>
<listitem>
<para>
If the optional <parameter>delimiter</parameter> is specified, it
will also be escaped. This is useful for escaping the delimiter
that is required by the PCRE functions. The / is the most commonly
used delimiter.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the quoted string.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>preg_quote</function> example</title>
@ -62,10 +96,13 @@ $textbody = preg_replace ("/" . preg_quote($word) . "/",
</programlisting>
</example>
</para>
&note.bin-safe;
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
&note.bin-safe;
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -1,12 +1,13 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.14 $ -->
<!-- $Revision: 1.15 $ -->
<refentry id="function.preg-replace-callback">
<refnamediv>
<refname>preg_replace_callback</refname>
<refpurpose>Perform a regular expression search and replace using a callback</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>preg_replace_callback</methodname>
<methodparam><type>mixed</type><parameter>pattern</parameter></methodparam>
@ -19,52 +20,46 @@
The behavior of this function is almost identical to
<function>preg_replace</function>, except for the fact that instead of
<parameter>replacement</parameter> parameter, one should specify a
<parameter>callback</parameter> that will be called and passed an array of
matched elements in the subject string. The callback should return the
replacement string.
See <function>preg_replace</function> for description of other parameters.
<parameter>callback</parameter>.
</para>
<example>
<title><function>preg_replace_callback</function> example</title>
<programlisting role='php'>
<![CDATA[
<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// the callback function
function next_year($matches)
{
// as usual: $matches[0] is the complete match
// $matches[1] the match for the first subpattern
// enclosed in '(...)' and so on
return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
"|(\d{2}/\d{2}/)(\d{4})|",
"next_year",
$text);
// result is:
// April fools day is 04/01/2003
// Last christmas was 12/24/2002
?>
]]>
</programlisting>
</example>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
You'll often need the <parameter>callback</parameter> function
for a <function>preg_replace_callback</function> in just one place.
In this case you can use <function>create_function</function> to
declare an anonymous function as callback within the call to
<function>preg_replace_callback</function>. By doing it this way
you have all information for the call in one place and do not
clutter the function namespace with a callback functions name
not used anywhere else.
</para>
<example>
<title><function>preg_replace_callback</function> and <function>create_function</function></title>
<programlisting role='php'>
<variablelist>
<varlistentry>
<term><parameter>pattern</parameter></term>
<listitem>
<para>
The pattern to search for. It can be either a string or an array with
strings.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>callback</parameter></term>
<listitem>
<para>
A callback that will be called and passed an array of matched elements
in the <parameter>subject</parameter> string. The callback should
return the replacement string.
</para>
<para>
You'll often need the <parameter>callback</parameter> function
for a <function>preg_replace_callback</function> in just one place.
In this case you can use <function>create_function</function> to
declare an anonymous function as callback within the call to
<function>preg_replace_callback</function>. By doing it this way
you have all information for the call in one place and do not
clutter the function namespace with a callback function's name
not used anywhere else.
</para>
<para>
<example>
<title><function>preg_replace_callback</function> and
<function>create_function</function></title>
<programlisting role="php">
<![CDATA[
<?php
/* a unix-style command line filter to convert uppercase
@ -87,12 +82,122 @@ while (!feof($fp)) {
fclose($fp);
?>
]]>
</programlisting>
</example>
<example>
<title><function>preg_replace_callback</function> using recursive structure
to handle encapsulated BB code</title>
<programlisting role='php'>
</programlisting>
</example>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>subject</parameter></term>
<listitem>
<para>
The string or an array with strings to search and replace.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>limit</parameter></term>
<listitem>
<para>
The maximum possible replacements for each pattern in each
<parameter>subject</parameter> string. Defaults to
<literal>-1</literal> (no limit).
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>count</parameter></term>
<listitem>
<para>
If specified, this variable will be filled with the number of
replacements done.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<function>preg_replace</function> returns an array if the
<parameter>subject</parameter> parameter is an array, or a string
otherwise.
</para>
<para>
If matches are found, the new <parameter>subject</parameter> will
be returned, otherwise <parameter>subject</parameter> will be
returned unchanged.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>5.1.0</entry>
<entry>
The <parameter>count</parameter> parameter was added
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>preg_replace_callback</function> example</title>
<programlisting role='php'>
<![CDATA[
<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// the callback function
function next_year($matches)
{
// as usual: $matches[0] is the complete match
// $matches[1] the match for the first subpattern
// enclosed in '(...)' and so on
return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
"|(\d{2}/\d{2}/)(\d{4})|",
"next_year",
$text);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
April fools day is 04/01/2003
Last christmas was 12/24/2002
]]>
</screen>
</example>
</para>
<para>
<example>
<title><function>preg_replace_callback</function> using recursive structure
to handle encapsulated BB code</title>
<programlisting role="php">
<![CDATA[
<?php
$input = "plain [indent] deep [indent] deeper [/indent] deep [/indent] plain";
@ -114,19 +219,22 @@ $output = parseTagsRecursive($input);
echo $output;
?>
]]>
</programlisting>
</example>
<note>
<para>
<parameter>count</parameter> parameter is available since PHP 5.1.0.
</para>
</note>
<para>
See also <function>preg_replace</function>,
<function>create_function</function>,
&listendand; &seealso.callback;.
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>preg_replace</function></member>
<member><function>create_function</function></member>
<member>&seealso.callback;</member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file

View file

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.20 $ -->
<!-- $Revision: 1.21 $ -->
<refentry id="function.preg-replace">
<refnamediv>
<refname>preg_replace</refname>
<refpurpose>Perform a regular expression search and replace</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
@ -57,9 +58,9 @@
otherwise PHP will complain about a parse error at the line containing
<function>preg_replace</function>.
</para>
</refsect1>
</refsect1>
<refsect1 role="parameters">
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
@ -146,9 +147,9 @@
</thead>
<tbody>
<row>
<entry>4.0.1</entry>
<entry>5.1.0</entry>
<entry>
Added the <parameter>limit</parameter> parameter
Added the <parameter>count</parameter> parameter
</entry>
</row>
<row>
@ -158,9 +159,9 @@
</entry>
</row>
<row>
<entry>5.1.0</entry>
<entry>4.0.1</entry>
<entry>
Added the <parameter>count</parameter> parameter
Added the <parameter>limit</parameter> parameter
</entry>
</row>
</tbody>

View file

@ -1,12 +1,13 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.12 $ -->
<!-- $Revision: 1.13 $ -->
<refentry id="function.preg-split">
<refnamediv>
<refname>preg_split</refname>
<refpurpose>Split string by a regular expression</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>preg_split</methodname>
<methodparam><type>string</type><parameter>pattern</parameter></methodparam>
@ -14,67 +15,134 @@
<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>.
Split the given string by a regular expression.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<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>.
<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, and if <parameter>limit</parameter> is -1, it actually
means "no limit", which is useful for specifying the
<parameter>flags</parameter>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<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.
</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>
<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>
Returns an array containing substrings of <parameter>subject</parameter>
split along boundaries matched by <parameter>pattern</parameter>.
</para>
</refsect1>
<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>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>4.3.0</entry>
<entry>
The <constant>PREG_SPLIT_OFFSET_CAPTURE</constant> was added
</entry>
</row>
<row>
<entry>4.0.5</entry>
<entry>
The <constant>PREG_SPLIT_DELIM_CAPTURE</constant> was added
</entry>
</row>
<row>
<entry>4 Beta 3</entry>
<entry>
The <parameter>flags</parameter> parameter was added
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<para>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>preg_split</function> example : Get the parts of a search string</title>
<programlisting role="php">
@ -114,9 +182,7 @@ print_r($chars);
?>
]]>
</programlisting>
<para>
will yield:
</para>
&example.outputs;
<screen>
<![CDATA[
Array
@ -144,18 +210,33 @@ Array
</screen>
</example>
</para>
<note>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<tip>
<para>
Parameter <parameter>flags</parameter> was added in PHP 4 Beta 3.
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>
</note>
</tip>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<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>.
<simplelist>
<member><function>spliti</function></member>
<member><function>split</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>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file