PHP Strings
A string is a collection of characters, such as "Welcome To PHP!".
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!":
Output :-
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!":
Output :-
Related Links
strrev() - Reverse a String
A string is reversed using the PHP strrev()
function.
Example :- Reverse the string "Welcome To PHP!":-
Output :-
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!":
Output :-
Tip :- The initial position in a string is 0. (not 1).
Related Links
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 "Simmanchith.com":
Output :-
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!