PHP Strings Tutorial
PHP provides a large number of string functions. If you are just learning PHP, reading through the list in the PHP manual can be a bit daunting. This tutorial organizes the most commonly used functions into categories and demonstrates their use in common string handling tasks. We cover:
- Basic information about strings in PHP.
- Information on variable type and type conversion in PHP.
- How to count the characters in a string or the number of words it contains.
- How to clean up strings, removing whitespace or other unwanted characters.
- How to convert strings to arrays.
- How to select and return a substring in a string.
- How to search for one string within another.
- How to replace substrings in strings.
- How to compare two strings using string functions or comparison operators.
- How to change the case of all or some of the characters in a string.
- How to display and format strings.
PHP String Functions
PHP string functions are listed below with brief descriptions and links to pages with more information and examples.
Function | Description | Page |
---|---|---|
strlen |
Count the number of characters in a string. | Count |
str_word_count |
Count words in a string or return array of words. | Count |
substr_count |
Count the number of times a substring occurs in a string. | Count |
strtolower |
Returns a string with all characters in lower case. | Change Case |
strtoupper |
Returns a string with all characters in upper case. | Change Case |
ucfirst |
Returns a string with the first letter in upper case. | Change Case |
lcfirst |
Returns a string with the first letter in lower case. | Change Case |
ucwords |
Returns a string with the first letter of each word in upper case. | Change Case |
substr |
Returns part of a string based on location and length. | Substr |
strpos |
Searches string; returns index location of match or false. | Search |
strrpos |
Searches for last match in string. | Search |
stripos |
Plus case insensitive versions of other search functions. | Search |
strstr |
Searches string. Returns from match to end of string. | Search |
strrchr |
Searches string from right for character. Returns from match to end. | Search |
str_replace |
Replace all instances of search string with replace string. | Replace |
str_ireplace |
Case insensitive search and replace. | Replace |
substr_replace |
Replace text at specified location in string. | Replace |
strtr |
Search and replace using key-value pairs or from-to strings. | Replace |
strcmp |
Compare two strings; return 0 if equal; >1 or <1 if not. | Compare |
strnatcmp |
String comparison using natural order algorithm. | Compare |
strncmp |
Compare initial character(s) of two strings. | Compare |
strcasecmp |
Plus case insensitive versions of other comparison functions. | Compare |
substr_compare |
Compare a string with part of another string. | Compare |
explode |
Split string into array elements on delimiter character(s). | Convert |
str_split |
Convert string to an array with elements of equal length. | Convert |
trim |
Remove whitespace or other characters from start/end of string | Cleanup |
rtrim |
rtrim /ltrim remove characters from right/left. |
Cleanup |
strip_tags |
Remove HTML and PHP tags from the string. | Cleanup |
echo |
Display string. print also. |
Display |
word_wrap |
Control line wrapping in string. | Display |
nl2br |
Convert new lines in string to <br /> tag. | Display |
number_format |
Format number with thousands separator and decimal places. | Display |
var_dump |
Display data, including value, data type, and length. | Display |