PHP String Function

Top 15 Most Common PHP String Functions

Posted by

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:

  1. strlen()
  2. str_replace()
  3. strstr()
  4. substr()
  5. strtolower()
  6. strtouper()
  7. str_word_count()
  8. strrev()
  9. strpos()
  10. ucwords()
  11. ucfirst()
  12. lcfirst()
  13. wordwrap()
  14. trim()
  15. 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:

Example:

Output:

Example 2:

Output:

2. str_replace() Function

We use str_replace function to replace some of the characters in a string with some other.

Syntax:

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:

Output: The string “Hello” has been replaced by “Hi”.

3. strstr() Function

We use strstr() function to display the string portion as of the first search occurrence.

Syntax:

Example:

Output:

Example:

Output:

4. substr() Function:

We use substr() function to extract the string.

Syntax:

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:

Output: In the above example, the output will be shown after 4 characters to the end of the strings.

Example:

Output: The output will be shown after 4 characters to 7 characters.

5. strtolower() Function

We use the strtolower() function to convert string to lower case. It takes one argument which will be a string.

Syntax:

Example:

Output: The string has been converted into a lower case.

6. strtouper() Function

The strtouper() function is used to convert string to lower case. It takes one argument which will be a string.

Syntax:

Example:

Output:

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:

Example:

Output:

8. Strrev() Function

The strrev() function is used to reverse the string in PHP. It takes a string as a parameter.

Syntax:

Output:

9. strpos() Function

We use the strpos() function to locate a string’s initial position within another string.

Syntax:

It takes three arguments:

String: Required. Sets search string.
Find: Required. Specifies find string.
Start: Optional. It specifies where the search should begin.

Example:

Output: Here is showing the position of the string “Reverse”.

10. ucwords() Function

The ucwords() function is used to convert the first letter of the word in upper case. It takes one argument.

Syntax:

Example:

Output: It has converted the first letter of each word in the upper case.

11. ucfirst() Function

We use the ucfirst() function to convert the first letter in upper case of string or sentence.

Syntax:

Example:

Output:

12. lcfirst() Function

We use the lcfirst() function to convert the first letter in lower case of string or sentence.

Syntax:

Example:

Output:

13. wordwrap() function

We use the wordwrap() function to wrap a given string to a certain number of characters.

Syntax:

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:

Output: In this example, the fourth parameter is set to TRUE, the function has divided the word after the specified width.

Example:

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.

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:

It accepts two parameters:

String: Required. Sets trimmed string.
Char-list: Optional. Sets character which will trim.

Example:

Output: This function has removed the whitespace from both sides on the string.

Example:

Output: It has trim “rt” from the start and “n” from the end.

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:

It accepts two parameters:

String: Required. Sets string to be strip.
Allow-Tags: Optional. Set the HTML tags which will not strip.

Example:

Output: It has cleared the opening and closing “<h3>” tag.

Example:

Output: It has stripped “<p>” tag but did not strip the “<h3>” tags.

You can also add multiple tags in the “Allow-Tags” parameter.

Example:

 

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.

Leave a Reply

Your email address will not be published. Required fields are marked *