<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Split a string by string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.echo.html">echo</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fprintf.html">fprintf</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.explode" class="refentry"> <div class="refnamediv"> <h1 class="refname">explode</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">explode</span> — <span class="dc-title">Split a string by 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="type">array</span> <span class="methodname"><strong>explode</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$delimiter</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$limit</code></span> ] )</div> <p class="para rdfs-comment"> Returns an array of strings, each of which is a substring of <em><code class="parameter">string</code></em> formed by splitting it on boundaries formed by the string <em><code class="parameter">delimiter</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.explode-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">delimiter</code></em></span> <dd> <p class="para"> The boundary string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">string</code></em></span> <dd> <p class="para"> The input string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">limit</code></em></span> <dd> <p class="para"> If <em><code class="parameter">limit</code></em> is set and positive, the returned array will contain a maximum of <em><code class="parameter">limit</code></em> elements with the last element containing the rest of <em><code class="parameter">string</code></em>. </p> <p class="para"> If the <em><code class="parameter">limit</code></em> parameter is negative, all components except the last -<em><code class="parameter">limit</code></em> are returned. </p> <p class="para"> If the <em><code class="parameter">limit</code></em> parameter is zero, then this is treated as 1. </p> </dd> </dt> </dl> </p> <p class="para"> Although <span class="function"><a href="function.implode.html" class="function">implode()</a></span> can, for historical reasons, accept its parameters in either order, <span class="function"><strong>explode()</strong></span> cannot. You must ensure that the <em><code class="parameter">delimiter</code></em> argument comes before the <em><code class="parameter">string</code></em> argument. </p> </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.html" class="type array">array</a></span> of <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s created by splitting the <em><code class="parameter">string</code></em> parameter on boundaries formed by the <em><code class="parameter">delimiter</code></em>. </p> <p class="para"> If <em><code class="parameter">delimiter</code></em> is an empty <span class="type"><a href="language.types.string.html" class="type string">string</a></span> (""), <span class="function"><strong>explode()</strong></span> will return <strong><code>FALSE</code></strong>. If <em><code class="parameter">delimiter</code></em> contains a value that is not contained in <em><code class="parameter">string</code></em> and a negative <em><code class="parameter">limit</code></em> is used, then an empty <span class="type"><a href="language.types.array.html" class="type array">array</a></span> will be returned, otherwise an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> containing <em><code class="parameter">string</code></em> will be returned. </p> </div> <div class="refsect1 changelog" id="refsect1-function.explode-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.1.0</td> <td> Support for negative <em><code class="parameter">limit</code></em>s was added </td> </tr> <tr> <td>4.0.1</td> <td> The <em><code class="parameter">limit</code></em> parameter was added </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.explode-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4828"> <p><strong>Example #1 <span class="function"><strong>explode()</strong></span> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?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: #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: #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: #FF8000">// foo<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$pass</span><span style="color: #007700">; </span><span style="color: #FF8000">// *<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4829"> <p><strong>Example #2 <span class="function"><strong>explode()</strong></span> return examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?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">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 /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(1) ( [0] => string(5) "hello" ) array(2) ( [0] => string(5) "hello" [1] => string(5) "there" ) </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4830"> <p><strong>Example #3 <em><code class="parameter">limit</code></em> parameter examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?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 (since PHP 5.1)<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">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [0] => one [1] => two|three|four ) Array ( [0] => one [1] => two [2] => 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 class="member"> <span class="function"><a href="function.preg-split.html" class="function" rel="rdfs-seeAlso">preg_split()</a> - Split string by a regular expression</span></li> <li class="member"> <span class="function"><a href="function.str-split.html" class="function" rel="rdfs-seeAlso">str_split()</a> - Convert a string to an array</span></li> <li class="member"> <span class="function"><a href="function.mb-split.html" class="function" rel="rdfs-seeAlso">mb_split()</a> - Split multibyte string using regular expression</span></li> <li class="member"> <span class="function"><a href="function.str-word-count.html" class="function" rel="rdfs-seeAlso">str_word_count()</a> - Return information about words used in a string</span></li> <li class="member"> <span class="function"><a href="function.strtok.html" class="function" rel="rdfs-seeAlso">strtok()</a> - Tokenize string</span></li> <li class="member"> <span class="function"><a href="function.implode.html" class="function" rel="rdfs-seeAlso">implode()</a> - Join array elements with a string</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.echo.html">echo</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fprintf.html">fprintf</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>