function.strtok.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 186 行
HTML
186 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Tokenize string</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><b><b>strtok</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$token</tt></span> )</div> <p class="para rdfs-comment"> <b>strtok()</b> splits a string (<i><tt class="parameter">str</tt></i>) into smaller strings (tokens), with each token being delimited by any character from <i><tt class="parameter">token</tt></i>. 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"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">str</tt></i></span> <dd> <p class="para"> The <a href="language.types.string.html" class="type string">string</a> being split up into smaller strings (tokens). </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">token</tt></i></span> <dd> <p class="para"> The delimiter used when splitting up <i><tt class="parameter">str</tt></i>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A <a href="language.types.string.html" class="type string">string</a> token. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>strtok()</b> example</b></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=$tok<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"> <p><b>Example #2 Old <b>strtok()</b> behavior</b></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"><pre><div class="cdata"><pre> string(0) "" string(9) "something"</pre></div> </pre></div> </div> <div class="example"> <p><b>Example #3 New <b>strtok()</b> behavior</b></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"><pre><div class="cdata"><pre> string(9) "something" bool(false)</pre></div> </pre></div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <div class="warning"><b class="warning">Warning</b><p class="simpara">This function mayreturn Boolean <b><tt>FALSE</tt></b>, but may also return a non-Boolean value whichevaluates to <b><tt>FALSE</tt></b>, such as <i>0</i> or"". Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for moreinformation. Use <a href="language.operators.comparison.html" class="link">the ===operator</a> for testing the return value of thisfunction.</p></div> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.split.html" class="function" rel="rdfs-seeAlso">split()</a></li> <li class="member"><a href="function.explode.html" class="function" rel="rdfs-seeAlso">explode()</a></li> </ul> </p> </div></div><hr /><div 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>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?