<!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>Tokenize string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strstr.html">strstr</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strtolower.html">strtolower</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.strtok" class="refentry"> <div class="refnamediv"> <h1 class="refname">strtok</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">strtok</span> — <span class="dc-title">Tokenize string</span></p> </div> <div class="refsect1 description" id="refsect1-function.strtok-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>strtok</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$token</code></span> )</div> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>strtok</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$token</code></span> )</div> <p class="para rdfs-comment"> <span class="function"><strong>strtok()</strong></span> splits a string (<em><code class="parameter">str</code></em>) into smaller strings (tokens), with each token being delimited by any character from <em><code class="parameter">token</code></em>. That is, if you have a string like "This is an example string" you could tokenize this string into its individual words by using the space character as the token. </p> <p class="para"> Note that only the first call to strtok uses the string argument. Every subsequent call to strtok only needs the token to use, as it keeps track of where it is in the current string. To start over, or to tokenize a new string you simply call strtok with the string argument again to initialize it. Note that you may put multiple tokens in the token parameter. The string will be tokenized when any one of the characters in the argument are found. </p> </div> <div class="refsect1 parameters" id="refsect1-function.strtok-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">str</code></em></span> <dd> <p class="para"> The <span class="type"><a href="language.types.string.html" class="type string">string</a></span> being split up into smaller strings (tokens). </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">token</code></em></span> <dd> <p class="para"> The delimiter used when splitting up <em><code class="parameter">str</code></em>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.strtok-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A <span class="type"><a href="language.types.string.html" class="type string">string</a></span> token. </p> </div> <div class="refsect1 examples" id="refsect1-function.strtok-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4905"> <p><strong>Example #1 <span class="function"><strong>strtok()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$string </span><span style="color: #007700">= </span><span style="color: #DD0000">"This is\tan example\nstring"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/* Use tab and newline as tokenizing characters as well */<br /></span><span style="color: #0000BB">$tok </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #DD0000">" \n\t"</span><span style="color: #007700">);<br /><br />while (</span><span style="color: #0000BB">$tok </span><span style="color: #007700">!== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Word=</span><span style="color: #0000BB">$tok</span><span style="color: #DD0000"><br />"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$tok </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">" \n\t"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> The behavior when an empty part was found changed with PHP 4.1.0. The old behavior returned an empty string, while the new, correct, behavior simply skips the part of the string: </p> <p class="para"> <div class="example" id="example-4906"> <p><strong>Example #2 Old <span class="function"><strong>strtok()</strong></span> behavior</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$first_token </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">'/something'</span><span style="color: #007700">, </span><span style="color: #DD0000">'/'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$second_token </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</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">$first_token</span><span style="color: #007700">, </span><span style="color: #0000BB">$second_token</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> string(0) "" string(9) "something" </pre></div> </div> </div> <div class="example" id="example-4907"> <p><strong>Example #3 New <span class="function"><strong>strtok()</strong></span> behavior</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$first_token </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">'/something'</span><span style="color: #007700">, </span><span style="color: #DD0000">'/'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$second_token </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</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">$first_token</span><span style="color: #007700">, </span><span style="color: #0000BB">$second_token</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> string(9) "something" bool(false) </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.strtok-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may return Boolean <strong><code>FALSE</code></strong>, but may also return a non-Boolean value which evaluates to <strong><code>FALSE</code></strong>. Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for more information. Use <a href="language.operators.comparison.html" class="link">the === operator</a> for testing the return value of this function.</p></div> </div> <div class="refsect1 seealso" id="refsect1-function.strtok-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.split.html" class="function" rel="rdfs-seeAlso">split()</a> - Split string into array by regular expression</span></li> <li class="member"> <span class="function"><a href="function.explode.html" class="function" rel="rdfs-seeAlso">explode()</a> - Split a string by 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.strstr.html">strstr</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strtolower.html">strtolower</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>