A string is a sequence of characters, like "Hello world!".
In this chapter we will look at some commonly used functions to manipulate strings.
The PHP strlen() function returns the length of a string (number of characters).
The example below returns the length of the string "Hello world!":
The output of the code above will be: 12.
The PHP str_word_count() function counts the number of words in a string:
The output of the code above will be: 2.
The PHP strrev() function reverses a string:
The output of the code above will be: !dlrow olleH.
The PHP 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.
The example below searches for the text "world" in the string "Hello world!":
The output of the code above will be: 6.
Tip: The first character position in a string is 0 (not 1).
The PHP str_replace() function replaces some characters with some other characters in a string.
The example below replaces the text "world" with "Dolly":
The output of the code above will be: Hello Dolly!
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!