strcspn – PHP String Functions

Syntax :

strcspn ( string, char, start, length);

Description :

strcspn() function will returns the number of characters found in a string before any part of the specified characters are found. It includes whitespaces also in counting.

Note : This function is binary-safe.

Parameter :

  • string – This is a Required parameter. It is the string which will be searched.
  • char – This is a Required parameter. It is character which is to be searched in string.
  • start – This is an Optional parameter. It tells from where the search is to be started in string.
  • length – This is an Optional parameter. It tells how much of the string is to be searched (length of the string to be searched).

Output :

It returns the number of characters found in a string before any part of the specified characters are found.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 4.3 , The start and length parameters was added in this version.
[/table]


Related articles : strspn().


strcspn() – PHP Functions Example 1 : There is a difference in search string “t” and “T”.
<?php
echo strcspn("Hi from Tutorialmines.",".");
echo "<br/>";
echo strcspn("Hi from Tutorialmines.","T");
echo "<br />";
echo strcspn("Hi from Tutorialmines.","t");
?>

Output of above code in the browser is as below:

21
8
10

strcspn() – PHP Functions Example 2 : With all parameters.
<?php
echo strcspn("Hi from Tutorialmines.",".");
echo "<br/>";
echo strcspn("Hi from Tutorialmines.","T",0,12);
echo "<br />";
echo strcspn("Hi from Tutorialmines.","n",5);
?>

Output of above code in the browser is as below:

21
8
13

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *