There are several ways to operate string using the built-in PHP functions. In this article, we will learn following top 15 most common PHP string functions:
- strlen()
- str_replace()
- strstr()
- substr()
- strtolower()
- strtouper()
- str_word_count()
- strrev()
- strpos()
- ucwords()
- ucfirst()
- lcfirst()
- wordwrap()
- trim()
- strip_tags()
1. strlen() Function
We use
strlen()
function to find the length of strings. It accepts only one parameter that will be the string whose length is to be obtained.
Syntax:
1 | strlen(String) |
Example:
1 2 3 4 | <?php $string = "Troposal"; echo strlen($string); ?> |
Output:
1 | 8 |
Example 2:
1 2 3 4 5 | <?php $string = "PHP is a programming language."; $length = strlen($string); echo $length; ?> |
Output:
1 | 30 |
2. str_replace() Function
We use
str_replace
function to replace some of the characters in a string with some other.
Syntax:
1 | str_replace(Search, Replace, Subject) |
This function accepts three arguments:
Search: Required. This will be the string which you have to search for replacing
Replace: Required. This will replace the searched string.
Subject: Required. This will the string which we have to search and replace.
Example:
1 2 3 4 5 6 7 | <?php $string = "Hello Troposal"; $search = "Hello"; $replace = "Hi"; $newString = str_replace($search, $replace, $string); echo $newString; ?> |
Output: The string “Hello” has been replaced by “Hi”.
1 | Hi Troposal |
3. strstr() Function
We use
strstr()
function to display the string portion as of the first search occurrence.
Syntax:
1 | strstr(haystack, needle) |
Example:
1 2 3 4 5 | <?php $string = "Hello Troposal"; $search = "ll"; echo strstr($string, $search); ?> |
Output:
1 | llo Troposal |
Example:
1 2 3 4 5 | <?php $string = "Hello Troposal"; $search = "ll"; echo strstr($string, $search, true); ?> |
Output:
1 | He |
4. substr() Function:
We use
substr()
function to extract the string.
Syntax:
1 | substr(String, Start, Length) |
It accepts three arguments:
String: This is the string we have to extract from. It is a mandatory field.
Start: Specifies where the string should start from.
- Positive number: Begin at a particular position in the string.
- Negative number: Begin at a particular position from the end of the string.
- Zero(0): Begin at the first character of the string.
Length: It is optional. Specifies the length of the string which you need to get. Default, It returns the end of the string.
Example:
1 2 3 4 | <?php $text = "Top 15 Most Common PHP String Functions"; echo substr($text, 4); ?> |
Output: In the above example, the output will be shown after 4 characters to the end of the strings.
1 | 15 Most Common PHP String Functions |
Example:
1 2 3 4 | <?php $text = "Top 15 Most Common PHP String Functions"; echo substr($text, 4, 7); ?> |
Output: The output will be shown after 4 characters to 7 characters.
1 | 15 Most |
5. strtolower() Function
We use the
strtolower()
function to convert string to lower case. It takes one argument which will be a string.
Syntax:
1 | strtolower(str); |
Example:
1 2 3 4 | <?php $text = "Top 15 Most Common PHP String Functions"; echo strtolower($text); ?> |
Output: The string has been converted into a lower case.
1 | top 15 most common php string functions |
6. strtouper() Function
The
strtouper()
function is used to convert string to lower case. It takes one argument which will be a string.
Syntax:
1 | strtoupper(string) |
Example:
1 2 3 4 | <?php $string = "PHP Strtoupper String Functions"; echo strtoupper($string) ?> |
Output:
1 | PHP STRTOUPPER STRING FUNCTIONS |
7. str_word_count() function
We use the
str_word_count()
function to count words from the string or sentence. It takes one argument which will be a string.
Syntax:
1 | str_word_count(string) |
Example:
1 2 3 4 | <?php $string = "PHP String Word Count Functions"; echo str_word_count($string); ?> |
Output:
1 | 5 |
8. Strrev() Function
The
strrev()
function is used to reverse the string in PHP. It takes a string as a parameter.
Syntax:
1 | strrev(string) |
1 2 3 4 | <?php $string = "PHP String reverse Functions"; echo strrev($string); ?> |
Output:
1 | snoitcnuF esrever gnirtS PHP |
9. strpos() Function
We use the
strpos()
function to locate a string’s initial position within another string.
Syntax:
1 | strpos(String, Find, Start) |
It takes three arguments:
String: Required. Sets search string.
Find: Required. Specifies find string.
Start: Optional. It specifies where the search should begin.
Example:
1 2 3 4 5 | <?php $string = "PHP String Reverse Functions"; $find = "Reverse"; echo strpos($string, $find); ?> |
Output: Here is showing the position of the string “Reverse”.
1 | 11 |
10. ucwords() Function
The
ucwords()
function is used to convert the first letter of the word in upper case. It takes one argument.
Syntax:
1 | ucwords(string) |
Example:
1 2 3 4 | <?php $string = "php string functions"; echo ucwords($string); ?> |
Output: It has converted the first letter of each word in the upper case.
1 | Php String Functions |
11. ucfirst() Function
We use the
ucfirst()
function to convert the first letter in upper case of string or sentence.
Syntax:
1 | ucfirst(string) |
Example:
1 2 3 4 | <?php $string = "string functions"; echo ucfirst($string); ?> |
Output:
1 | String functions |
12. lcfirst() Function
We use the
lcfirst()
function to convert the first letter in lower case of string or sentence.
Syntax:
1 | lcfirst(string) |
Example:
1 2 3 4 | <?php $string = "PHP String Functions"; echo lcfirst($string); ?> |
Output:
1 | pHP String Functions |
13. wordwrap() function
We use the
wordwrap()
function to wrap a given string to a certain number of characters.
Syntax:
1 | wordwrap(String, Width, Break, Cut) |
It takes four parameters:
String: Required. Sets wrapped string.
Width: It is the number of integers the string breaks after.
Break: Optional. It adds value to the point where the string is broken.
Cut: It is a boolean parameter, If this parameter is set to TRUE, the string is wrapped after the specified width. If this parameter is set to FALSE the function will not divide the word even if the width is less than the word width.
Example:
1 2 3 4 | <?php $string = "Wrap up the string using this function"; echo wordwrap($string, 4, "\n", TRUE); ?> |
Output: In this example, the fourth parameter is set to TRUE, the function has divided the word after the specified width.
1 2 3 4 5 6 7 8 9 10 | Wrap up the stri ng usin g this func tion |
Example:
1 2 3 4 | <?php $string = "Wrap up the string using this function"; echo wordwrap($string, 4, "\n", false); ?> |
Output: In this example, the fourth parameter is set to FALSE, the function has not divided the word even if the width is less than the word width.
1 2 3 4 5 6 7 | Wrap up the string using this function |
14. trim() function
The Trim() function is used for trimming the white space from the start and end of the strings. Also, it removes the character from string if you set in parameter.
Syntax:
1 | trim(String, Char-list) |
It accepts two parameters:
String: Required. Sets trimmed string.
Char-list: Optional. Sets character which will trim.
Example:
1 2 3 4 5 | <?php //Added whitespace in both side of string $string = " Wrap up the string using this function "; echo trim($string); ?> |
Output: This function has removed the whitespace from both sides on the string.
1 | Wrap up the string using this function |
Example:
1 2 3 4 | <?php $string = "trim string using this function"; echo trim($string, 'trn'); ?> |
Output: It has trim “rt” from the start and “n” from the end.
1 | im string using this functio |
There are two more functions which trim the white space or other predefine characters:
ltrim()
Function
It removes white space or other predefined characters from the left side of a string.
rtrim()
Function
It removes white space or other predefined characters from the right side of a string.
15. strip_tags() Function
We use the
strip_tags()
function to clear the HTML tags.
Syntax:
1 | strip_tags(String, Allow-Tags) |
It accepts two parameters:
String: Required. Sets string to be strip.
Allow-Tags: Optional. Set the HTML tags which will not strip.
Example:
1 2 3 4 | <?php $string = "<h3>Strip HTML tags using this function</h3>"; echo strip_tags($string); ?> |
Output: It has cleared the opening and closing “<h3>” tag.
1 | Strip HTML tags using this function |
Example:
1 2 3 4 | <?php $string = "<h3><p>Strip HTML tags using this function</p></h3>"; echo strip_tags($string, "<h3>"); ?> |
Output: It has stripped “<p>” tag but did not strip the “<h3>” tags.
1 | <h3>Strip HTML tags using this function</h3> |
You can also add multiple tags in the “Allow-Tags” parameter.
Example:
1 2 3 4 | <?php $string = "<h3><p><i>Strip HTML tags using this function</i></p></h3>"; echo strip_tags($string, "<h3><p>"); ?> |
We have covered Top 15 Most Common PHP String Functions which use frequently in the development of PHP application. If you wish to share more information or have a query about the topic Top 15 Most Common PHP String Functions, please write comments.