A String is simply a sequence of characters like ‘THIS IS PHP!’. PHP contains a huge variety of string functions. The PHP string functions basically is an important part of the PHP Core. PHP contains near about one hundred string functions to manipulate different strings. Some of the common functions that can be performed on strings are:
- Comparing two strings.
- Find string in another string.
- Returning a part of the string.
- Finding instances of the string in another string.
- Changing entire string from lowercase to uppercase and vise-versa.
- Trimming white spaces from the end of the string.
- Strip HTML and PHP tags from within the string.
Below is the list of fifty useful string functions, along with required parameters and a brief description.
Function(Parameters) | Description |
echo(string) | Outputs the 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() |
Strlen
It returns the length of the string.
echo strlen(“Life is beautiful if you see it”); // outputs 31
?>
Strpos
It returns the position of specific text in the string.
Strops(“string”, “what to find?”);
CODE
Search omelet in the string.
$string = “You can’t make an omelet without breaking a few eggs.”;
$position = strpos($string, “omelet”);
if ($position === false) {
echo “Omelet not found in the given string”;
} else {
echo “Omelet found in the given string”;
}
?>
OUTPUT
SUBSTR
It returns the part of the string.
Substr(“string”, “startpos”, “lengthofsubstr”);
CODE
$string = “You can’t make an omelet without breaking a few eggs.”;
$substr = substr($string, “18”, “14”);
echo $substr;
?>
Str_replace
It replaces the all occurrences of a string in a string.
Str_replace(“whattoreplace”, “replacewith”, “string”);
CODE
$string = “You can’t make an omelet without breaking a few eggs.”;
$strreplace = str_replace(“an”, “a”, $string);
echo $strreplace;
?>
OUTPUT
In the above example you can see that “an” has been replaced by “a” in the string “You can’t make an omelet without breaking a few eggs.”
strstr
It checks for the first occurrence of the string.
$string = “hukma,charles & David”;
$strstr = strstr($string,”,”);
echo $strstr;
?>
OUTPUT
In this above example, we are checking “,”. So it is showing the first occurrence of “,” and data after that.
Strcmp
CODE
$var1 = “God Loves you, as you love your children”;
$var2 = “God Loves you, as you love your children”;
echo strcmp($var1, $var2);
?>
OUTPUT
If the string matches, then it returns 0
Str_spilt
Splits the string into array.
Str_split(“string”, “Split length”);
CODE
$str = “God is one”;
$array = str_split($str, 3);
print_r($array);
?>
OUTPUT
Leave a comment