<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.strings.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.explode.php',
    1 => 'explode',
    2 => 'Split a string by a string',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String Functions',
  ),
  'prev' => 
  array (
    0 => 'function.echo.php',
    1 => 'echo',
  ),
  'next' => 
  array (
    0 => 'function.fprintf.php',
    1 => 'fprintf',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/strings/functions/explode.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

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

 </div>
 
 <div class="refsect1 description" id="refsect1-function.explode-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>explode</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$separator</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string</code></span>, <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"> = <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong></span></span>): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <p class="para rdfs-comment">
   Returns an array of strings, each of which is a substring of
   <code class="parameter">string</code> formed by splitting it on
   boundaries formed by the string <code class="parameter">separator</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.explode-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">separator</code></dt>
     <dd>
      <p class="para">
       The boundary string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">string</code></dt>
     <dd>
      <p class="para">
       The input string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">limit</code></dt>
     <dd>
      <p class="para">
       If <code class="parameter">limit</code> is set and positive, the returned array will contain
       a maximum of <code class="parameter">limit</code> elements with the last
       element containing the rest of <code class="parameter">string</code>.
      </p>
      <p class="para">
       If the <code class="parameter">limit</code> parameter is negative, all components
       except the last -<code class="parameter">limit</code> are returned.
      </p>
      <p class="para">
       If the <code class="parameter">limit</code> parameter is zero, then this is treated as 1.
      </p>
     </dd>
    
   </dl>
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Prior to PHP 8.0, <span class="function"><a href="function.implode.php" class="function">implode()</a></span> accepted its parameters in either order.
    <span class="function"><strong>explode()</strong></span> has never supported this: you must ensure that the
    <code class="parameter">separator</code> argument comes before the
    <code class="parameter">string</code> argument.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.explode-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of <span class="type"><a href="language.types.string.php" class="type string">string</a></span>s
   created by splitting the <code class="parameter">string</code> parameter on
   boundaries formed by the <code class="parameter">separator</code>.
  </p>
  <p class="para">
   If <code class="parameter">separator</code> is an empty <span class="type"><a href="language.types.string.php" class="type string">string</a></span> (&quot;&quot;),
   <span class="function"><strong>explode()</strong></span> throws a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>.
   If <code class="parameter">separator</code> contains a value that is not
   contained in <code class="parameter">string</code> and a negative
   <code class="parameter">limit</code> is used, then an empty <span class="type"><a href="language.types.array.php" class="type array">array</a></span> will be
   returned, otherwise an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> containing
   <code class="parameter">string</code> will be returned. If <code class="parameter">separator</code> 
   values appear at the start or end of <code class="parameter">string</code>, said values 
   will be added as an empty <span class="type"><a href="language.types.array.php" class="type array">array</a></span> value either in the first or last 
   position of the returned <span class="type"><a href="language.types.array.php" class="type array">array</a></span> respectively.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.explode-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <span class="function"><strong>explode()</strong></span> will now throw <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span>
       when <code class="parameter">separator</code> parameter is given an empty string
       (<code class="literal">&quot;&quot;</code>).
       Previously, <span class="function"><strong>explode()</strong></span> returned <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> instead.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.explode-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>explode()</strong></span> examples</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">// Example 1<br /></span><span style="color: #0000BB">$pizza  </span><span style="color: #007700">= </span><span style="color: #DD0000">"piece1 piece2 piece3 piece4 piece5 piece6"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$pieces </span><span style="color: #007700">= </span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">" "</span><span style="color: #007700">, </span><span style="color: #0000BB">$pizza</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$pieces</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// piece1<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$pieces</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// piece2<br /><br />// Example 2<br /></span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #DD0000">"foo:*:1023:1000::/home/foo:/bin/sh"</span><span style="color: #007700">;<br />list(</span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">$pass</span><span style="color: #007700">, </span><span style="color: #0000BB">$uid</span><span style="color: #007700">, </span><span style="color: #0000BB">$gid</span><span style="color: #007700">, </span><span style="color: #0000BB">$gecos</span><span style="color: #007700">, </span><span style="color: #0000BB">$home</span><span style="color: #007700">, </span><span style="color: #0000BB">$shell</span><span style="color: #007700">) = </span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">":"</span><span style="color: #007700">, </span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// foo<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$pass</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// *<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 <span class="function"><strong>explode()</strong></span> return examples</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">/* <br />   A string that doesn't contain the delimiter will simply<br />   return a one-length array of the original string.<br />*/<br /></span><span style="color: #0000BB">$input1 </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$input2 </span><span style="color: #007700">= </span><span style="color: #DD0000">"hello,there"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$input3 </span><span style="color: #007700">= </span><span style="color: #DD0000">','</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">( </span><span style="color: #0000BB">explode</span><span style="color: #007700">( </span><span style="color: #DD0000">','</span><span style="color: #007700">, </span><span style="color: #0000BB">$input1 </span><span style="color: #007700">) );<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">( </span><span style="color: #0000BB">explode</span><span style="color: #007700">( </span><span style="color: #DD0000">','</span><span style="color: #007700">, </span><span style="color: #0000BB">$input2 </span><span style="color: #007700">) );<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">( </span><span style="color: #0000BB">explode</span><span style="color: #007700">( </span><span style="color: #DD0000">','</span><span style="color: #007700">, </span><span style="color: #0000BB">$input3 </span><span style="color: #007700">) );<br /><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(1)
(
    [0] =&gt; string(5) &quot;hello&quot;
)
array(2)
(
    [0] =&gt; string(5) &quot;hello&quot;
    [1] =&gt; string(5) &quot;there&quot;
)
array(2)
(
    [0] =&gt; string(0) &quot;&quot;
    [1] =&gt; string(0) &quot;&quot;
)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Example #3 <code class="parameter">limit</code> parameter examples</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">'one|two|three|four'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// positive limit<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">explode</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">2</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// negative limit<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">explode</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">));<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; one
    [1] =&gt; two|three|four
)
Array
(
    [0] =&gt; one
    [1] =&gt; two
    [2] =&gt; three
)</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.explode-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function is
binary-safe.</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.explode-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.preg-split.php" class="function" rel="rdfs-seeAlso">preg_split()</a> - Split string by a regular expression</span></li>
    <li><span class="function"><a href="function.str-split.php" class="function" rel="rdfs-seeAlso">str_split()</a> - Convert a string to an array</span></li>
    <li><span class="function"><a href="function.mb-split.php" class="function" rel="rdfs-seeAlso">mb_split()</a> - Split multibyte string using regular expression</span></li>
    <li><span class="function"><a href="function.str-word-count.php" class="function" rel="rdfs-seeAlso">str_word_count()</a> - Return information about words used in a string</span></li>
    <li><span class="function"><a href="function.strtok.php" class="function" rel="rdfs-seeAlso">strtok()</a> - Tokenize string</span></li>
    <li><span class="function"><a href="function.implode.php" class="function" rel="rdfs-seeAlso">implode()</a> - Join array elements with a string</span></li>
   </ul>
  </p>
 </div>


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