Explain some string functions of PHP
In the programming world, a string is considered a data type, which in general is a sequence of multiple characters that can contain whitespaces, numbers, characters, and special symbols as well. For example, “Hello World!”, “ID-34#90” etc. PHP also allows single quotes(‘ ‘) for defining a string. Every programming language provides some in-built functions for the manipulation of strings. Some of the basic string functions provided by PHP are as follows:
strlen() Function: It returns the length of the string i.e. the count of all the characters in the string including whitespaces characters.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
strlen(string or variable name)
strrev() Function: It returns the reversed string of the given string.
strrev(string or variable name)
trim(), ltrim(), rtrim(), and chop() Functions: It remove white spaces or other characters from the string. They have two parameters: one string and another charList, which is a list of characters that need to be omitted.
- trim() – Removes characters or whitespaces from both sides.
- rtrim() & chop() – Removes characters or whitespaces from right side.
- ltrim() – Removes characters or whitespaces from the left side.
Note: The browser output of the code given in the examples below may vary from HTML output for these functions.
rtrim(string, charList) ltrim(string, charList) trim(string, charList) chop(string, charList)
- $string: This mandatory parameter specifies the string to be checked.
- $charlist: This optional parameter specifies which characters are to be removed from the string. In case, this parameter is not provided, the following characters are removed :
- “\0” – NULL
- “\t” – tab
- “\n” – new line
- “\x0B” – vertical tab
- “\r” – carriage return
- ” “ – ordinary white space
Note – The parameter charList is available only in PHP version 4.1 or higher.
This is an example for string functions. This is an example for string functions.<br>This is an example for string functions.<br> This is an example for string functions.<br>This is an example for string functions.
- strtoupper() – It returns the string after converting all the letters to uppercase.
- strtolower() – It returns the string after converting all the letters to lowercase.
str_split() Function: It returns an array containing parts of the string.
- string: It specifies the string to be checked, it can also be a variable name of type string.
- length: It specifies the length of each part of the string to be stored in the string, by default, it is 1. If the length is larger than the size of the string, then the complete string is returned.
Array (  => G  => e  => e  => k  => s  => F  => o  => r  => G  => e  => e  => k  => s ) Array (  => Gee  => ksF  => orG  => eek  => s )