PHP strcspn() Function
In this chapter you will learn:
- Definition for PHP strcspn() Function
- Syntax for PHP strcspn() Function
- Parameter for PHP strcspn() Function
- Return for PHP strcspn() Function
- Example - Print the number of characters found in "Hello world!" before the character "w"
- Example - Set the start and length to search
Definition
The strcspn() function returns the number of characters including whitespaces found in a string before the index of the specified characters.
Syntax
PHP strcspn() Function has the following syntax.
strcspn(string,char,start,length)
Parameter
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to search |
char | Required. | Characters to search for |
start | Optional. | Where in string to start |
length | Optional. | Length of the string to search |
Return
PHP strcspn() Function returns the number of characters found in a string before the specified characters
Example 1
Print the number of characters found in "Hello world!" before the character "w":
<?php
echo strcspn("Hello world from java2s.com!","w");
?>
The code above generates the following result.
Example 2
Set the start and length to search and print the number of characters found in "Hello world!" before the character "w":
<?php
echo strcspn("Hello world from java2s.com!","w",0,15);
// The start position is 0 and the length of the search string is 15.
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP strip_tags() Function
- Syntax for PHP strip_tags() Function
- Parameter for PHP strip_tags() Function
- Return for PHP strip_tags() Function
- Example - Strip HTML tags
Home » PHP Tutorial » PHP String Functions