25 Extremely Useful PHP String Functions

PHP

There are a lot of php string functions and they are very useful. These functions are used to perform operations with string values. You can change cases, search within strings, count the characters, trim them etc.

Here is the list of some extremely useful php string functions along with their usage examples:

strtoupper() and strtolower()
These two functions convert a string value into uppercase or lowercase. The entire string is converted.

Example:

<?php
$a="String value";
echo strtoupper($a)."<br />";
echo strtolower($a);
?>
Output:
STRING VALUE
string value

ucfirst(), ucwords() and lcfirst()
ucfirst() and ucwords() are used to convert certain characters of strings to uppercase. ucfirst() converts the first character of a string to uppercase. ucwords() does it for all the words of the string. lcfirst() converts the first character of a string to lowercase.

Example:

<?php
$a="string value";
echo ucfirst($a)."<br />";
echo ucwords($a)."<br />";
echo lcfirst($a);
?>
Output:
String value
String Value
string value

strlen()
This php function is used to determine the length of a string.

Example:

<?php
$a="String value";
echo strlen($a);
?>
Output:
12

trim(),ltrim(),rtrim()
These three functions are used to trim or remove whitespace as well as other characters of a string. trim() removes the characters from both sides of a string, ltrim() does it from the left side and rtrim() removes from the right side. They are all case sensitive.

Example:

<?php
$a="String value";
echo ltrim($a,"String")."<br />";
echo rtrim($a,"value")."<br />";
echo trim($a,"String");
?>
Output:
value
String
value

substr()
This php function is used to return parts of the string. You can remove portions of a string value.

Example:

<?php
$a = "String value";
echo substr($a,3)."<br />";
echo substr($a,-3)."<br />";
echo substr($a,3,-3)."<br />";
?>
Output:
ing value
lue
ing va

strpos()
If you need to find the position of a certain string within another string, use this function. You will find out the first occurrence of the string.

Example:

<?php
$a = "String value String value";
echo strpos($a,"value");
?>
Output:
7

strchr()
Like strpos(), this one is used to find a string within another string. This one finds the actual string and not its position. The first occurrence is considered.

Example:

<?php
$a = "String value String value";
echo strpos($a,"value");
?>
Output:
value String value

addslashes() and stripslashes()
The function addslashes() adds backslashes() to certain characters in a string that needs to be escaped. Characters such as single quote(‘), double quote(“), backslash () etc. have backslashes added before them. The function stripslashes removes such slashes. Useful for entering values of a string into and from MySQL databases.

Example:

<?php
$a = "String's value";
echo addslashes($a)."<br />";
echo stripslashes($a);
?>
Output:
String's value
String's value

htmlentities() and htmlspecialchars()  html_entity_decode() and htmlspecialchars_decode()
You can convert special characters into HTML representations with the function htmlentities() and htmlspecialchars(). You perform their reverse with html_entity_decode() and htmlspecialchars_decode().

Example:

<?php
$a = "String & Value";
echo htmlentities($a);//& becomes &amp;
echo htmlspecialchars($a);//& becomes &amp;
?>

similar_text()
Compares two strings and calculates how much similar they are. The total number of similar characters are added up.

<?php
$a = "String value";
$b = "String";
echo similar_text($a,$b);
?>
Output:
6

str_word_count()
Simply counts the number of words in a string.

Example:

<?php
$a = "Sting value";
echo str_word_count($a);
?>
Output:
2

explode()
This function breaks down a string into a single array. You can specify the character to break them down by.

Example:

<?php
$a = "String,value,remember";
$b = explode(",",$a);
print_r($b);
?>
Output:
Array ( [0] => String [1] => value [2] => remember )

implode()
The opposite of explode to convert an array into a string.

Example:

<?php
$a = array("String","value","remember");
$b = implode(",",$a);
echo $b;
?>
Output:
String,value,remember

str_replace()
This function replaces occurences of strings within another string.

For example:

<?php
$a = "String value";
echo str_replace("String","King",$a);
?>
Output:
King value

strip_tags()
This function removes HTML tags from a string value.

Example:

<?php
$a = "String <br />value";
echo strip_tags($a);
?>
Output:
String value

sha1()
This function is used to find out the SHA-1 hash of a string. Useful for storing passwords.

Example:

<?php
$a = "password";
echo sha1($a);
?>
Output:
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8

Click here to view the entire list of php functions from the official source.