PHP provides nearly one hundred functions that can manipulate strings in various ways. Some (but not all!) of the functions that can be performed on strings are:
Compare Two Strings
Find a String In Another String
Find Out How Many Instances of A String Occur In Another String
Return Part of a String
Replace Part of a String
Trim Whitespace From The Ends of a String
Make An Entire String Lowercase or Uppercase
Convert All Applicable Characters In a String to HTML Entities, or Vice Versa
Strip HTML & PHP Tags From Inside of a String
The simplest string functions are those that only require a single parameter. This parameter is the string itself, which is accepted either as a variable or as a single or double-quoted string. The strlen() function, for example, will return the length of the string that it is given.
<?php
$string = "What is the definition of a caterpillar?";
echo strlen($string); // Result Will Be 40
echo strlen("A worm in a fur coat!"); // Result Will Be 21
$string_length = strlen($string);
// The Length of $string (40) Is Now Stored In $string_length
?>
As you can see, the results of these functions can be echoed, stored in a variable, etc.
Most functions accept multiple parameters. The strpos() function, for example, searches for occurrences of a string inside of another string, and requires two parameters in order to function. Multiple parameter are separated by commas.
<?php
$str = "There were four cats in a boat, one jumped out. How many were left?";
echo strpos($str, "cat"); // Result Will Be 16
?>
The strpos() function will return a number indicating the position of the first match that it encounters. If you begin to do the counting yourself, you will see that the "c" for "cat" is the 17th character... so why did the function say 16? This is because most PHP functions begin counting with 0, so the position of the string's first character is 0, the second character is 1, etc.
(By the way, there were no cats left in the boat, because they were all copy cats.)
Below is a list of fifty useful string functions, along with a brief description and required parameters.
Function(Parameters) | Description |
---|---|
echo(string) | Outputs Strings |
print(string) | Outputs a String |
printf(string) | Outputs a Formatted String |
ltrim(string) | Strips Whitespace From the Left Side of a String |
rtrim(string) | Strips Whitespace From the Right Side of a String |
trim(string) | Strips Whitespace From Both Sides of a String |
lcfirst(string) | Makes a String's First Character Lowercase |
ucfirst(string) | Makes a String's First Character Uppercase |
strtolower(string) | Converts a String to Lowercase Letters |
strtoupper(string) | Converts a String to Uppercase Letters |
str_word_count(string) | Count the Number of Words In a String |
ucwords(string) | Makes the First Character of Each Word In a String Uppercase |
wordwrap(string, width, break) | Wraps a String to a Given Number of Characters (Default Width: 75) (Default Break: \n) |
count_chars(string) | Returns How Many Times an ASCII Character Occurs Within a String & Returns the Information |
substr_count(string, substring) | Counts the Number of Times a Substring Occurs In a String |
str_pad(string, length, pad_string) | Pads a String to a New Length |
strlen(string) | Returns the Length of a String |
substr(string, start) | Returns a Part of a String (Start Value of "0" to Begin at First Character) |
strrev(string) | Reverses a String |
str_shuffle(string) | Randomly Shuffles All Characters In a String |
str_repeat(string, repeat) | Repeats a String a Specified Number of Times ("Repeat" Is Number of Times to Repeat) |
strpbrk(string, characters) | Searches a String For Any of a Set of Characters |
str_replace(find, replace, string) | Replaces Some Characters In a String (Case-Sensitive) |
substr_replace(string, replacement, start) | Replaces a Part of a String With Another String |
stristr(string, search) | Finds the First Occurrence of a String Inside Another String (Case-Insensitive) |
strstr(string, search) | Finds the First Occurrence of a String Inside Another String (Case-Sensitive) |
strrchr(string, char) | Finds the Last Occurrence of a String Inside Another String |
stripos(string, find) | Returns the Position of the First Occurrence of a String Inside Another String (Case-Insensitive) |
strpos(string, find) | Returns the Position of the First Occurrence of a String Inside Another String (Case-Sensitive) |
strripos(string, find) | Returns the Position of the Last Occurrence of a String Inside Another String (Case-Insensitive) |
strrpos(string, find) | Returns the Position of the Last Occurrence of a String Inside Another String (Case-Sensitive) |
strcasecmp(string1, string2) | Compares Two Strings (Case-Insensitive) |
strcmp(string1, string2) | Compares Two Strings (Case-Sensitive) |
strtok(string, split) | Splits a String Into Smaller Strings |
chunk_split(string, length) | Splits a String Into a Series of Smaller Parts (Default Length Is 76) |
str_split(string, length) | Splits a String Into an Array |
explode(separator, string) | Breaks a String Into an Array |
implode(separator, array) | Returns a String From the Elements of an Array |
str_getcsv(string, delimiter, enclosure) | Parses a CSV String Into an Array |
addcslashes(string) | Returns a String With Backslashes In Front of Single Quotes, Double Quotes & Backslashes |
stripcslashes(string) | Unquotes a String Quoted With addcslashes() |
addslashes(string,characters) | Returns a String With Backslashes in Front of Predefined Characters |
stripslashes(string) | Unquotes a String Quoted With addslashes() |
nl2br(string) | Inserts HTML Line Breaks In Front of Each Newline In a String |
strip_tags(string) | Strips HTML & PHP Tags From a String |
html_entity_decode(string) | Converts HTML Entities to Characters |
htmlentities(string) | Converts Characters to HTML Entities |
htmlspecialchars_decode(string) | Converts Some Predefined HTML Entities to Characters |
htmlspecialchars(string) | Converts Some Predefined Characters to HTML Entities |
get_html_translation_table() | Returns the Translation Table Used by htmlspecialchars() & htmlentities() |
A complete list of the string functions available can be found in the official PHP manual. Each function includes examples that will help you understand how to use it.