<!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>Find length of initial segment not matching mask</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strcoll.html">strcoll</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strip-tags.html">strip_tags</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.strcspn" class="refentry"> <div class="refnamediv"> <h1 class="refname">strcspn</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">strcspn</span> — <span class="dc-title">Find length of initial segment not matching mask</span></p> </div> <div class="refsect1 description" id="refsect1-function.strcspn-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>strcspn</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"> Returns the length of the initial segment of <code class="parameter">subject</code> which does <em class="emphasis">not</em> contain any of the characters in <code class="parameter">mask</code>. </p> <p class="para"> If <code class="parameter">start</code> and <code class="parameter">length</code> are omitted, then all of <code class="parameter">subject</code> will be examined. If they are included, then the effect will be the same as calling <em>strcspn(substr($subject, $start, $length), $mask)</em> (see <a href="function.substr.html" class="xref">substr</a> for more information). </p> </div> <div class="refsect1 parameters" id="refsect1-function.strcspn-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">subject</code></dt> <dd> <p class="para"> The string to examine. </p> </dd> <dt> <code class="parameter">mask</code></dt> <dd> <p class="para"> The string containing every disallowed character. </p> </dd> <dt> <code class="parameter">start</code></dt> <dd> <p class="para"> The position in <code class="parameter">subject</code> to start searching. </p> <p class="para"> If <code class="parameter">start</code> is given and is non-negative, then <span class="function"><strong>strcspn()</strong></span> will begin examining <code class="parameter">subject</code> at the <code class="parameter">start</code>'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 <code class="parameter">start</code> is given and is negative, then <span class="function"><strong>strcspn()</strong></span> will begin examining <code class="parameter">subject</code> at the <code class="parameter">start</code>'th position from the end of <code class="parameter">subject</code>. </p> </dd> <dt> <code class="parameter">length</code></dt> <dd> <p class="para"> The length of the segment from <code class="parameter">subject</code> to examine. </p> <p class="para"> If <code class="parameter">length</code> is given and is non-negative, then <code class="parameter">subject</code> will be examined for <code class="parameter">length</code> characters after the starting position. </p> <p class="para"> If <code class="parameter">length</code> is given and is negative, then <code class="parameter">subject</code> will be examined from the starting position up to <code class="parameter">length</code> characters from the end of <code class="parameter">subject</code>. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.strcspn-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the length of the initial segment of <code class="parameter">subject</code> which consists entirely of characters <em class="emphasis">not</em> in <code class="parameter">mask</code>. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> When a <code class="parameter">start</code> parameter is set, the returned length is counted starting from this position, not from the beginning of <code class="parameter">subject</code>. </p> </p></blockquote> </div> <div class="refsect1 examples" id="refsect1-function.strcspn-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-6094"> <p><strong>Example #1 <span class="function"><strong>strcspn()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcd'</span><span style="color: #007700">, </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcd'</span><span style="color: #007700">, </span><span style="color: #DD0000">'banana'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">, </span><span style="color: #DD0000">'l'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$d </span><span style="color: #007700">= </span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">, </span><span style="color: #DD0000">'world'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$e </span><span style="color: #007700">= </span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdhelloabcd'</span><span style="color: #007700">, </span><span style="color: #DD0000">'abcd'</span><span style="color: #007700">, -</span><span style="color: #0000BB">9</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$f </span><span style="color: #007700">= </span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdhelloabcd'</span><span style="color: #007700">, </span><span style="color: #DD0000">'abcd'</span><span style="color: #007700">, -</span><span style="color: #0000BB">9</span><span style="color: #007700">, -</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$f</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(0) int(2) int(2) int(5) int(4) </pre></div> </div> </div> </div> <div class="refsect1 notes" id="refsect1-function.strcspn-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.strcspn-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.strspn.html" class="function" rel="rdfs-seeAlso">strspn()</a> - Finds the length of the initial segment of a string consisting entirely of characters contained within a given 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.strcoll.html">strcoll</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strip-tags.html">strip_tags</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>