PHP String Functions
PHP has many built-in string functions.
PHP strlen() Function
The strlen() function returns the length of a string.
Example
Return the length of the string "Hello world!":
echo strlen("Hello world!");
Try it Yourself »
PHP str_word_count() Function
The str_word_count() function counts the
number of words in a string.
Example
Count the number of word in the string "Hello world!":
echo str_word_count("Hello world!");
Try it Yourself »
PHP str_contains() Function
The
str_contains() function checks if a string contains a specific
substring.
Tip: The str_contains()
function is only available in PHP 8.0 and later. For older versions, use the
strpos()
function.
If a match is found, the function returns a boolean true. If no match is found, it will return a boolean false.
Example
Search for the text "love" in the string "I really love PHP!":
$txt = "I really love PHP!";
var_dump(str_contains($txt, "love"));
Try it Yourself »
Note: This function performs a case-sensitive search.
The following example will return a boolean false, because "Love" is not found in the main string:
Example
Search for the text "Love" in the string "I really love PHP!":
$txt = "I really love PHP!";
var_dump(str_contains($txt, "Love"));
Try it Yourself »
PHP strpos() Function
The strpos() function searches for a specific text within a string.
If a match is found, the function returns the character position of the first match. If no match is found, it will return false.
Note: This function performs a case-sensitive search.
Example
Search for the text "world" in the string "Hello world!":
echo strpos("Hello world!", "world");
Try it Yourself »
Tip: The first character position in a string is 0 (not 1).
PHP str_starts_with() Function
The
str_starts_with() function checks if a string starts with a specific substring.
Tip: The str_starts_with()
function is only available in PHP 8.0 and later.
If a match is found, the function returns a boolean true. If no match is found, it will return a boolean false.
Example
Check if a string starts with a specific substring:
$txt = "I really love PHP!";
var_dump(str_starts_with($txt, "I really"));
Try it Yourself »
Note: This function performs a case-sensitive search.
The following example will return a boolean false, because "i really" is not found in the main string:
Example
Showing case-sensitivity:
$txt = "I really love PHP!";
var_dump(str_starts_with($txt, "i really"));
Try it Yourself »
PHP str_ends_with() Function
The
str_ends_with() function checks if a string ends with a specific substring.
Tip: The str_ends_with()
function is only available in PHP 8.0 and later.
If a match is found, the function returns a boolean true. If no match is found, it will return a boolean false.
Example
Check if a string ends with a specific substring:
$txt = "I really love PHP!";
var_dump(str_ends_with($txt, "PHP!"));
Try it Yourself »
Note: This function performs a case-sensitive search.
The following example will return a boolean false, because "php!" is not found in the main string:
Example
Showing case-sensitivity:
$txt = "I really love PHP!";
var_dump(str_ends_with($txt, "php!"));
Try it Yourself »
Complete PHP String Reference
For a complete reference of all string functions, go to our complete PHP String Reference.