<!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>Finds the length of the initial segment of a string consisting entirely of characters contained within a given mask.</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strrpos.html">strrpos</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strstr.html">strstr</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.strspn" class="refentry"> <div class="refnamediv"> <h1 class="refname">strspn</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">strspn</span> — <span class="dc-title"> Finds the length of the initial segment of a string consisting entirely of characters contained within a given mask. </span></p> </div> <div class="refsect1 description" id="refsect1-function.strspn-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>strspn</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$subject</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$mask</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$start</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span> ]] )</div> <p class="para rdfs-comment"> Finds the length of the initial segment of <em><code class="parameter">subject</code></em> that contains <em class="emphasis">only</em> characters from <em><code class="parameter">mask</code></em>. </p> <p class="para"> If <em><code class="parameter">start</code></em> and <em><code class="parameter">length</code></em> are omitted, then all of <em><code class="parameter">subject</code></em> will be examined. If they are included, then the effect will be the same as calling <em>strspn(substr($subject, $start, $length), $mask)</em> (see <a href="function.substr.html" class="xref">substr</a> for more information). </p> <p class="para"> The line of code: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$var </span><span style="color: #007700">= </span><span style="color: #0000BB">strspn</span><span style="color: #007700">(</span><span style="color: #DD0000">"42 is the answer to the 128th question."</span><span style="color: #007700">, </span><span style="color: #DD0000">"1234567890"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> will assign <em>2</em> to <var class="varname"><var class="varname">$var</var></var>, because the string "42" is the initial segment of <em><code class="parameter">subject</code></em> that consists only of characters contained within "1234567890". </p> </div> <div class="refsect1 parameters" id="refsect1-function.strspn-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">subject</code></em></span> <dd> <p class="para"> The string to examine. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">mask</code></em></span> <dd> <p class="para"> The list of allowable characters. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">start</code></em></span> <dd> <p class="para"> The position in <em><code class="parameter">subject</code></em> to start searching. </p> <p class="para"> If <em><code class="parameter">start</code></em> is given and is non-negative, then <span class="function"><strong>strspn()</strong></span> will begin examining <em><code class="parameter">subject</code></em> at the <em><code class="parameter">start</code></em>'th position. For instance, in the string '<em>abcdef</em>', the character at position <em>0</em> is '<em>a</em>', the character at position <em>2</em> is '<em>c</em>', and so forth. </p> <p class="para"> If <em><code class="parameter">start</code></em> is given and is negative, then <span class="function"><strong>strspn()</strong></span> will begin examining <em><code class="parameter">subject</code></em> at the <em><code class="parameter">start</code></em>'th position from the end of <em><code class="parameter">subject</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">length</code></em></span> <dd> <p class="para"> The length of the segment from <em><code class="parameter">subject</code></em> to examine. </p> <p class="para"> If <em><code class="parameter">length</code></em> is given and is non-negative, then <em><code class="parameter">subject</code></em> will be examined for <em><code class="parameter">length</code></em> characters after the starting position. </p> <p class="para"> If <em><code class="parameter">length</code></em>is given and is negative, then <em><code class="parameter">subject</code></em> will be examined from the starting position up to <em><code class="parameter">length</code></em> characters from the end of <em><code class="parameter">subject</code></em>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.strspn-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the length of the initial segment of <em><code class="parameter">subject</code></em> which consists entirely of characters in <em><code class="parameter">mask</code></em>. </p> </div> <div class="refsect1 changelog" id="refsect1-function.strspn-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>4.3.0</td> <td> The <em><code class="parameter">start</code></em> and <em><code class="parameter">length</code></em> parameters were added </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.strspn-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4903"> <p><strong>Example #1 <span class="function"><strong>strspn()</strong></span> example</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">// subject does not start with any characters from mask<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">strspn</span><span style="color: #007700">(</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">, </span><span style="color: #DD0000">"o"</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// examine two characters from subject starting at offset 1<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">strspn</span><span style="color: #007700">(</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">, </span><span style="color: #DD0000">"o"</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// examine one character from subject starting at offset 1<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">strspn</span><span style="color: #007700">(</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">, </span><span style="color: #DD0000">"o"</span><span style="color: #007700">, </span><span style="color: #0000BB">1</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> int(0) int(2) int(1) </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.strspn-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.strspn-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.strcspn.html" class="function" rel="rdfs-seeAlso">strcspn()</a> - Find length of initial segment not matching mask</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.strrpos.html">strrpos</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strstr.html">strstr</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>