Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 2290

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>preg_match_all</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="Regular Expression Functions (Perl-Compatible)"
HREF="ref.pcre.html"><LINK
REL="PREVIOUS"
TITLE="preg_grep"
HREF="function.preg-grep.html"><LINK
REL="NEXT"
TITLE="preg_match"
HREF="function.preg-match.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="refentry"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.preg-grep.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.preg-match.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.preg-match-all"
></A
>preg_match_all</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN81471"
></A
><P
>    (PHP 3&#62;= 3.0.9, PHP 4 )</P
>preg_match_all&nbsp;--&nbsp;Perform a global regular expression match</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN81474"
></A
><H2
>Description</H2
>int <B
CLASS="methodname"
>preg_match_all</B
> ( string pattern, string subject, array matches [, int flags])<BR
></BR
><P
>&#13;     Searches <TT
CLASS="parameter"
><I
>subject</I
></TT
> for all matches to the regular
     expression given in <TT
CLASS="parameter"
><I
>pattern</I
></TT
> and puts them in
     <TT
CLASS="parameter"
><I
>matches</I
></TT
> in the order specified by
     <TT
CLASS="parameter"
><I
>flags</I
></TT
>.
    </P
><P
>&#13;     After the first match is found, the subsequent searches  are continued
     on from end of the last match.
    </P
><P
>&#13;     <TT
CLASS="parameter"
><I
>flags</I
></TT
> can be a combination of the following flags
     (note that it doesn't make sense to use
     <TT
CLASS="constant"
><B
>PREG_PATTERN_ORDER</B
></TT
> together with
     <TT
CLASS="constant"
><B
>PREG_SET_ORDER</B
></TT
>):
     <P
></P
><DIV
CLASS="variablelist"
><DL
><DT
>PREG_PATTERN_ORDER</DT
><DD
><P
>&#13;         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.
         <DIV
CLASS="informalexample"
><A
NAME="AEN81506"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
preg_match_all ("|&#60;[^&#62;]+&#62;(.*)&#60;/[^&#62;]+&#62;|U", 
    "&#60;b&#62;example: &#60;/b&#62;&#60;div align=left&#62;this is a test&#60;/div&#62;", 
    $out, PREG_PATTERN_ORDER);
print $out[0][0].", ".$out[0][1]."\n";
print $out[1][0].", ".$out[1][1]."\n";
?&#62;</PRE
></TD
></TR
></TABLE
><P
>&#13;           This example will produce:
           <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>&#60;b&#62;example: &#60;/b&#62;, &#60;div align=left&#62;this is a test&#60;/div&#62;
example: , this is a test</PRE
></TD
></TR
></TABLE
>
           So, $out[0] contains array of strings that matched full pattern,
           and $out[1] contains array of strings enclosed by tags.
          </P
><P
></P
></DIV
>
        </P
></DD
><DT
>PREG_SET_ORDER</DT
><DD
><P
>&#13;         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.
         <DIV
CLASS="informalexample"
><A
NAME="AEN81514"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
preg_match_all ("|&#60;[^&#62;]+&#62;(.*)&#60;/[^&#62;]+&#62;|U", 
    "&#60;b&#62;example: &#60;/b&#62;&#60;div align=left&#62;this is a test&#60;/div&#62;", 
    $out, PREG_SET_ORDER);
print $out[0][0].", ".$out[0][1]."\n";
print $out[1][0].", ".$out[1][1]."\n";
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
         This example will produce:
         <DIV
CLASS="informalexample"
><A
NAME="AEN81516"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;b&#62;example: &#60;/b&#62;, example: 
&#60;div align=left&#62;this is a test&#60;/div&#62;, this is a test</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
         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.
        </P
></DD
><DT
>PREG_OFFSET_CAPTURE</DT
><DD
><P
>&#13;         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 <TT
CLASS="literal"
>0</TT
> and it's string offset
         into <TT
CLASS="parameter"
><I
>subject</I
></TT
> at offset <TT
CLASS="literal"
>1</TT
>.
         This flag is available since <TT
CLASS="literal"
>PHP</TT
> 4.3.0 .
        </P
></DD
></DL
></DIV
>
    </P
><P
>&#13;     If no order flag is given, <TT
CLASS="constant"
><B
>PREG_PATTERN_ORDER</B
></TT
> is
     assumed.
    </P
><P
>&#13;     Returns the number of full pattern matches (which might be zero),
     or <TT
CLASS="constant"
><B
>FALSE</B
></TT
> if an error occurred.
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN81531"
></A
><P
><B
>Example 1. Getting all phone numbers out of some text.</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
preg_match_all ("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x",
                "Call 555-1212 or 1-800-555-1212", $phones);
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN81535"
></A
><P
><B
>Example 2. Find matching HTML tags (greedy)</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
// The \\2 is an example of backreferencing. This tells pcre that
// it must match the second set of parentheses in the regular expression
// itself, which would be the ([\w]+) in this case. The extra backslash is 
// required because the string is in double quotes.
$html = "&#60;b&#62;bold text&#60;/b&#62;&#60;a href=howdy.html&#62;click me&#60;/a&#62;";

preg_match_all ("/(&#60;([\w]+)[^&#62;]*&#62;)(.*)(&#60;\/\\2&#62;)/", $html, $matches);

for ($i=0; $i&#60; count($matches[0]); $i++) {
  echo "matched: ".$matches[0][$i]."\n";
  echo "part 1: ".$matches[1][$i]."\n";
  echo "part 2: ".$matches[3][$i]."\n";
  echo "part 3: ".$matches[4][$i]."\n\n";
}
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     This example will produce:
     <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>matched: &#60;b&#62;bold text&#60;/b&#62;
part 1: &#60;b&#62;
part 2: bold text
part 3: &#60;/b&#62;

matched: &#60;a href=howdy.html&#62;click me&#60;/a&#62;
part 1: &#60;a href=howdy.html&#62;
part 2: click me
part 3: &#60;/a&#62;</PRE
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     See also <A
HREF="function.preg-match.html"
><B
CLASS="function"
>preg_match()</B
></A
>,
     <A
HREF="function.preg-replace.html"
><B
CLASS="function"
>preg_replace()</B
></A
>,
     and <A
HREF="function.preg-split.html"
><B
CLASS="function"
>preg_split()</B
></A
>.
    </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.preg-grep.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.preg-match.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>preg_grep</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.pcre.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>preg_match</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>