Current Location: Home> Latest Articles> PHP strcspn Usage Examples: Finding the Position of the First Non-Matching Character

PHP strcspn Usage Examples: Finding the Position of the First Non-Matching Character

gitbox 2025-09-02
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Example of PHP code unrelated to the article</span></span><span>
</span><span><span class="hljs-variable">$placeholder</span></span><span> = </span><span><span class="hljs-string">"This is some PHP code unrelated to the article"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$placeholder</span></span><span>;
</span><span><span class="hljs-meta">?&gt;</span></span><span>
<p><hr></p>
<p></span><?php<br>
/**</p>
<ul>
<li>
<p>PHP strcspn Usage Examples: Finding the Position of the First Non-Matching Character</p>
</li>
<li></li>
<li>
<p>The strcspn() function is used to find the position of the first character in a string</p>
</li>
<li>
<p>that does not match any character from a specified set.</p>
</li>
<li>
<p>Syntax:</p>
</li>
<li>
<p>int strcspn(string $string, string $characters, int $start = 0, int $length = ?)</p>
</li>
<li></li>
<li>
<p>Parameters:</p>
</li>
<li>
<ul>
<li>
<p>$string: The string to search.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$characters: The string containing the characters to match.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$start: Optional. The starting position for the search.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$length: Optional. The length of the segment to search.</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>Return value:</p>
</li>
<li>
<p>Returns the position (0-based index) of the first character not contained in the specified set.<br>
*/</p>
</li>
</ul>
<p>// Example 1: Simple search<br>
$text = "hello world";<br>
$chars = "aeiou"; </span>// Set of vowels to search<br>
$position = strcspn($text, $chars);<br>
echo "Example 1: In the string '{$text}', the position of the first character not matching '{$chars}' is: {$position}<br>";</p>
<p>// Example 2: Search from a specific position<br>
$text2 = "abcdefg123";<br>
$chars2 = "1234567890"; // Set of digits<br>
$position2 = strcspn($text2, $chars2, 3); // Search starting from index 3<br>
echo "Example 2: In the string '{$text2}', starting from position 3, the first character not matching a digit is at: {$position2}<br>";</p>
<p>// Example 3: With length limit<br>
$text3 = "php is fun!";<br>
$chars3 = "pih"; // Set of characters<br>
$position3 = strcspn($text3, $chars3, 0, 5); // Only check the first 5 characters<br>
echo "Example 3: In the first 5 characters of '{$text3}', the position of the first character not matching '{$chars3}' is: {$position3}<br>";</p>
<p>// Summary<br>
echo "<br>Summary:<br>";<br>
echo "strcspn() is a very useful function that quickly locates the position of the first character in a string not included in the specified set.";<br>
?></p>
<p data-is-last-node="" data-is-only-node=""><?php<br>
// Footer PHP code unrelated to the article<br>
$footer = "End of unrelated code demonstration in the article footer";<br>
echo "<hr>".$footer;<br>
?><br>