Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP String strcspn() Function



The PHP String strcspn() function is a built-in PHP function that returns the number of characters in a string before any of the characters specified for search are found. This function is case-sensitive.

Syntax

Below is the syntax of the PHP String strcspn() function −

int strcspn (
   string $string,
   string $characters,
   int $offset = 0,
   ?int $length = null
)

Parameters

Here are the parameters of the strcspn() function −

  • $string − (Required) It is used to specify the string to search.

  • $characters − (Required) It is used to specify the characters to search for.

  • $offset − (Optional) It is used to specify where in string to start. Positive means start from offset position. Negative means start from offset from the end.

  • $length − (Optional) It is used to specify the length of the string. Positive means search up to length characters from offset. Negative means search until length characters from the end.

Return Value

The strcspn() function returns the total number of characters found in a string before any of the specified characters are found.

PHP Version

First introduced in core PHP 4, the strcspn() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP String strcspn() function to get the first occurrence of the characters which is not allowed in a string.

<?php
   // Mention your string here
   $string = "hello world";

   // Define character here
   $characters = "aeiou";

   // Use strcspn() function
   $result = strcspn($string, $characters);

   // Print the result
   echo "Length of initial segment without vowels: $result\n";
?>

Output

Here is the outcome of the following code −

Length of initial segment without vowels: 1

Example 2

In the below PHP code we will use the strcspn() function and find for disallowed characters starting from a given position.

<?php
   // Mention your string here
   $string = "hello tutorialspoint";
   $characters = "aeiou";
   $offset = 6;

   // Use strcspn() function
   $result = strcspn($string, $characters, $offset);

   // Print the result
   echo "Length from position $offset without vowels: $result\n";
?> 

Output

This will generate the below output −

Length from position 6 without vowels: 1

Example 3

Now the below code searches for vowels starting from the third character and checks only the next 5 characters (length = 5).

<?php
   // Mention your string here
   $string = "hello tutorialspoint";
   $characters = "aeiou";
   $offset = 2;
   $length = 5;

   // Use strcspn() function
   $result = strcspn($string, $characters, $offset, $length);
   
   // Print the result
   echo "Length of segment without vowels: $result\n";
?> 

Output

This will create the below output −

Length of segment without vowels: 2

Example 4

In the following example, we are using the strcspn() function to search for both uppercase and lowercase vowels in a substring.

<?php
   // Mention your string here
   $string = "Hard work is the key to success, but no pain, no gain.";
   $characters = "aeiouAEIOU";
   $offset = 10;
   $length = 15;
   $result = strcspn($string, $characters, $offset, $length);

   // Print the result
   echo "Length of segment without vowels (case-insensitive): $result\n";
?> 

Output

Following is the output of the above code −

Length of segment without vowels (case-insensitive): 0
php_function_reference.htm
Advertisements