25 Extremely Useful PHP String Functions


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:

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:

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

Example:

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:

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

Example:

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:

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:

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:

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:

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

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

Example:

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

Example:

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

Example:

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

For example:

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

Example:

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

Example:

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

You may also like

Comments are closed here.

Advertisements

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers