<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.pcre.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.preg-split.php',
    1 => 'preg_split',
    2 => 'Split string by a regular expression',
  ),
  'up' => 
  array (
    0 => 'ref.pcre.php',
    1 => 'PCRE Functions',
  ),
  'prev' => 
  array (
    0 => 'function.preg-replace-callback-array.php',
    1 => 'preg_replace_callback_array',
  ),
  'next' => 
  array (
    0 => 'book.ssdeep.php',
    1 => 'ssdeep',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pcre/functions/preg-split.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.preg-split" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_split</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">preg_split</span> &mdash; <span class="dc-title">Split string by a regular expression</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.preg-split-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>preg_split</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$pattern</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$subject</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$limit</code><span class="initializer"> = -1</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span><br>): <span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   Split the given string by a regular expression.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.preg-split-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">pattern</code></dt>
     <dd>
      <p class="para">
       The pattern to search for, as a string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">subject</code></dt>
     <dd>
      <p class="para">
       The input string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">limit</code></dt>
     <dd>
      <p class="para">
       If specified, then only substrings up to <code class="parameter">limit</code>
       are returned with the rest of the string being placed in the last
       substring.  A <code class="parameter">limit</code> of -1 or 0 means &quot;no limit&quot;.
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">flags</code> can be any combination of the following
       flags (combined with the <code class="literal">|</code> bitwise operator):
       <dl>
        
         <dt><strong><code><a href="pcre.constants.php#constant.preg-split-no-empty">PREG_SPLIT_NO_EMPTY</a></code></strong></dt>
         <dd>
          <span class="simpara">
          If this flag is set, only non-empty pieces will be returned by
          <span class="function"><strong>preg_split()</strong></span>.
          </span>
         </dd>
        
        
         <dt><strong><code><a href="pcre.constants.php#constant.preg-split-delim-capture">PREG_SPLIT_DELIM_CAPTURE</a></code></strong></dt>
         <dd>
          <span class="simpara">
          If this flag is set, parenthesized expression in the delimiter pattern
          will be captured and returned as well.
          </span>
         </dd>
        
        
         <dt><strong><code><a href="pcre.constants.php#constant.preg-split-offset-capture">PREG_SPLIT_OFFSET_CAPTURE</a></code></strong></dt>
         <dd>
          <p class="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 <code class="literal">0</code> and its string offset
           into <code class="parameter">subject</code> at offset <code class="literal">1</code>.
          </p>
         </dd>
        
       </dl>
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-split-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns an array containing substrings of <code class="parameter">subject</code>
   split along boundaries matched by <code class="parameter">pattern</code>,  or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.preg-split-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
If the regex pattern passed does not compile to a valid regex, an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is emitted.
</p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-split-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>preg_split()</strong></span> example : Get the parts of a search string</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// split the phrase by any number of commas or space characters,<br />// which include " ", \r, \t, \n and \f<br /></span><span style="color: #0000BB">$keywords </span><span style="color: #007700">= </span><span style="color: #0000BB">preg_split</span><span style="color: #007700">(</span><span style="color: #DD0000">"/[\s,]+/"</span><span style="color: #007700">, </span><span style="color: #DD0000">"hypertext language, programming"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$keywords</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Array
(
    [0] =&gt; hypertext
    [1] =&gt; language
    [2] =&gt; programming
)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 Splitting a string into component characters</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'string'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$chars </span><span style="color: #007700">= </span><span style="color: #0000BB">preg_split</span><span style="color: #007700">(</span><span style="color: #DD0000">'//'</span><span style="color: #007700">, </span><span style="color: #0000BB">$str</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PREG_SPLIT_NO_EMPTY</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$chars</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Array
(
    [0] =&gt; s
    [1] =&gt; t
    [2] =&gt; r
    [3] =&gt; i
    [4] =&gt; n
    [5] =&gt; g
)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Example #3 Splitting a string into matches and their offsets</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'hypertext language programming'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$chars </span><span style="color: #007700">= </span><span style="color: #0000BB">preg_split</span><span style="color: #007700">(</span><span style="color: #DD0000">'/ /'</span><span style="color: #007700">, </span><span style="color: #0000BB">$str</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">PREG_SPLIT_OFFSET_CAPTURE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$chars</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Array
(
    [0] =&gt; Array
        (
            [0] =&gt; hypertext
            [1] =&gt; 0
        )

    [1] =&gt; Array
        (
            [0] =&gt; language
            [1] =&gt; 10
        )

    [2] =&gt; Array
        (
            [0] =&gt; programming
            [1] =&gt; 19
        )

)</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.preg-split-notes">
  <h3 class="title">Notes</h3>
  <div class="tip"><strong class="tip">Tip</strong>
   <p class="para">
    If you don&#039;t need the power of regular expressions, you can choose
    faster (albeit simpler) alternatives like <span class="function"><a href="function.explode.php" class="function">explode()</a></span>
    or <span class="function"><a href="function.str-split.php" class="function">str_split()</a></span>.
   </p>
  </div>
  <div class="tip"><strong class="tip">Tip</strong>
   <p class="para">
    If matching fails, an array with a single element containing the input string will be returned.
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.preg-split-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="pcre.pattern.php" class="link">PCRE Patterns</a></li>
    <li><span class="function"><a href="function.preg-quote.php" class="function" rel="rdfs-seeAlso">preg_quote()</a> - Quote regular expression characters</span></li>
    <li><span class="function"><a href="function.explode.php" class="function" rel="rdfs-seeAlso">explode()</a> - Split a string by a string</span></li>
    <li><span class="function"><a href="function.preg-match.php" class="function" rel="rdfs-seeAlso">preg_match()</a> - Perform a regular expression match</span></li>
    <li><span class="function"><a href="function.preg-match-all.php" class="function" rel="rdfs-seeAlso">preg_match_all()</a> - Perform a global regular expression match</span></li>
    <li><span class="function"><a href="function.preg-replace.php" class="function" rel="rdfs-seeAlso">preg_replace()</a> - Perform a regular expression search and replace</span></li>
    <li><span class="function"><a href="function.preg-last-error.php" class="function" rel="rdfs-seeAlso">preg_last_error()</a> - Returns the error code of the last PCRE regex execution</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>