Current Location: Home> Function Categories> strcspn

strcspn

Get the length of the starting substring that does not match the mask
Name:strcspn
Category:String
Programming Language:php
One-line Description:Returns the number of characters found in the string before any part of some specified characters is found.

Example

Example 1

Output the number of characters found before the character "w" is found in the string "Hello world!":

 <span class="token php language-php"><span class="token delimiter important"><?php</span>
<span class="token keyword">echo</span> <span class="token function">strcspn</span> <span class="token punctuation">(</span> <span class="token string double-quoted-string">"Hello world!"</span> <span class="token punctuation">,</span> <span class="token string double-quoted-string">"w"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span>
<span class="token delimiter important">?></span></span>

Try it yourself

Example 2

Use all parameters to output the number of characters that were found before the character "w" is found in the string "Hello world!":

 <span class="token php language-php"><span class="token delimiter important"><?php</span>
<span class="token keyword">echo</span> <span class="token function">strcspn</span> <span class="token punctuation">(</span> <span class="token string double-quoted-string">"Hello world!"</span> <span class="token punctuation">,</span> <span class="token string double-quoted-string">"w"</span> <span class="token punctuation">,</span> <span class="token number">0</span> <span class="token punctuation">,</span> <span class="token number">6</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// The start position is 0, and the length of the search string is 6.</span>
<span class="token delimiter important">?></span></span>

Try it yourself