<HTML ><HEAD ><TITLE >preg_split</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_replace" HREF="function.preg-replace.html"><LINK REL="NEXT" TITLE="qtdom functions" HREF="ref.qtdom.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-replace.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="ref.qtdom.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.preg-split" ></A >preg_split</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN81772" ></A ><P > (PHP 3>= 3.0.9, PHP 4 )</P >preg_split -- Split string by a regular expression</DIV ><DIV CLASS="refsect1" ><A NAME="AEN81775" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >preg_split</B > ( string pattern, string subject [, int limit [, int flags]])<BR ></BR ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Parameter <TT CLASS="parameter" ><I >flags</I ></TT > was added in PHP 4 Beta 3. </P ></BLOCKQUOTE ></DIV ><P > Returns an array containing substrings of <TT CLASS="parameter" ><I >subject</I ></TT > split along boundaries matched by <TT CLASS="parameter" ><I >pattern</I ></TT >. </P ><P > If <TT CLASS="parameter" ><I >limit</I ></TT > is specified, then only substrings up to <TT CLASS="parameter" ><I >limit</I ></TT > are returned, and if <TT CLASS="parameter" ><I >limit</I ></TT > is -1, it actually means "no limit", which is useful for specifying the <TT CLASS="parameter" ><I >flags</I ></TT >. </P ><P > <TT CLASS="parameter" ><I >flags</I ></TT > can be any combination of the following flags (combined with bitwise | operator): <P ></P ><DIV CLASS="variablelist" ><DL ><DT >PREG_SPLIT_NO_EMPTY</DT ><DD ><P > If this flag is set, only non-empty pieces will be returned by <B CLASS="function" >preg_split()</B >. </P ></DD ><DT >PREG_SPLIT_DELIM_CAPTURE</DT ><DD ><P > 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. </P ></DD ><DT >PREG_SPLIT_OFFSET_CAPTURE</DT ><DD ><P > 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 > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81824" ></A ><P ><B >Example 1. <B CLASS="function" >preg_split()</B > example : Get the parts of a search string.</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="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");</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81829" ></A ><P ><B >Example 2. Splitting a string into component characters.</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$str = 'string'; $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81833" ></A ><P ><B >Example 3. Splitting a string into matches and their offsets.</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$str = 'hypertext language programming'; $chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE); print_r($chars);</PRE ></TD ></TR ></TABLE ><P > will yield </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [0] => Array ( [0] => hypertext [1] => 0 ) [1] => Array ( [0] => language [1] => 10 ) [2] => Array ( [0] => programming [1] => 19 ) )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.spliti.html" ><B CLASS="function" >spliti()</B ></A >, <A HREF="function.split.html" ><B CLASS="function" >split()</B ></A >, <A HREF="function.implode.html" ><B CLASS="function" >implode()</B ></A >, <A HREF="function.preg-match.html" ><B CLASS="function" >preg_match()</B ></A >, <A HREF="function.preg-match-all.html" ><B CLASS="function" >preg_match_all()</B ></A >, and <A HREF="function.preg-replace.html" ><B CLASS="function" >preg_replace()</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-replace.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="ref.qtdom.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >preg_replace</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" >qtdom functions</TD ></TR ></TABLE ></DIV ></BODY ></HTML >