PHP Strings

A string is a collection of characters, such as "Welcome To PHP!".

You can also search for these topics, define string in php, php string example, php print the string, php string declarations, php string functions.

PHP String Functions

In this section, we will examine some widely used string handling functions.

strlen() - Return the Length of a String

The strlen() function in PHP calculates a string's length.

Example :-Return the length of the string "Welcome To PHP!":

echo strlen("Welcome To PHP!"); // outputs 15

Output :-


You can also search for these topics, php strlen() functions, php string length, php string length limit, Return the length of a string in php, example of strlen() function, find the length of the string using php.

str_word_count() - Count Words in a String

The str_word_count() function in PHP determines how many words are in a string.

Example :- Count the number of word in the string "Welcome To PHP!":

echo str_word_count("Welcome To PHP!"); // outputs 3

Output :-


You can also search for these topics, php str_word_count() function, php str count word, count the string in php, php string count occurance, php str_word_count() example, count the words in the string using php.

strrev() - Reverse a String

A string is reversed using the PHP strrev() function.

Example :- Reverse the string "Welcome To PHP!":-

echo strrev("Welcome To PHP!"); // outputs !PHP oT emocleW

Output :-

!PHP oT emocleW

You can also search for these topics, php strrev() function, reverse the string using php, php reverse the string, example of strrev() function, strrev php reverse.

strpos() - Search For a Text Position Within a String

The PHP strpos() function looks for index or position of specified content within a string. If a match is found, the method returns the character index position of the first match. If there is no match, it returns FALSE.

Example :- Search for the text "PHP" in the string "Welcome To PHP!":

echo strpos("Welcome To PHP!", "PHP"); // outputs 12

Output :-


Tip :- The initial position in a string is 0. (not 1).

You can also search for these topics, php strpos() function, search as text within a string using php, find the particular text within string using php, php strpos() example, php strpos case insensitive, how to search a text in php.

str_replace() - Replace Text Within a String

In a string, the PHP str_replace() function substitutes or replace certain characters for others in a string.

Example :- Replace the text "PHP" with "":

echo str_replace("PHP", "", "Welcome To PHP!"); // outputs Welcome To!

Output :-

Welcome To!

You can also search for these topics, php str_replace() function, replace text within a string in php, php str_replace() example, how to replace the string in php.

Complete PHP String Reference

For a complete reference of all string functions, go to our complete PHP String Reference.

The PHP string reference contains description and example of use, for each function!