Frequently used PHP String Functions with Example & Explanation

In PHP there are more than hundred of String functions available freely to make string operations easier. But you must noticed during our day today development frequently we use some of the String functions. Normally those string function are String replace, Sub String, String length, String Position or Trim a String. For fresher in PHP let us discuss all those frequently used String function with examples. It’s not matter if you don’t know all PHP string functions, when required you can take the help of Google to recall the Syntax. But to be a good programmer it’s mandatory for you to remember below PHP string functions.

ltrim(), rtrim() & trim String Functions

Assume that you have a string with whitespace or predefined characters at the beginning and end. In this case to remove whitespaces or predefined characters trim function is useful. Compare to ltrim and rtrim trim function helps to reduce spaces and predefined characters from the both side (left and right) of a string. While ltrim removes whitespaces & predefined characters from left part of a string rtrim helps to remove whitespaces & predefined characters from the right side of a string. Look at the example below.

<?php
$str = "Hari is a good Boy.";
echo ltrim($str,"Hari");
?>

Output: is a good Boy.

str_replace() Function

In PHP we use str_replace() function to replace specific characters with some other characters. This function is case sensitive. If in a string you want replace a specific word str_replace function replace all the words with similar characters. Syntax for str_replace is as below.

Syntax: str_replace(find, replace, string, count);

Keep remember str_replace accepts 4 parameters. The first 3 parameters are mandatory. Where ever the last parameter count is optional. The first parameter find says which string of characters you want to replace. While replace says what are the other characters you want to replace for the find characters. String parameter is the variable which holds the complete string. Count is optional. In place of count if you are passing any variable as a parameter it returns the total number of time the find word replaced. Look at the example below here I have a string “Hello Biswabhusan!”. I want to replace “Biswabhusan” with “Peter”. To know how many times the word “Biswabhusan” was replaced I am passing $i as the count parameter and printing that after a hyphen.

<?php
echo str_replace("Biswabhusan","Peter","Hello Biswabhusan!", $i);
echo "-" . $i;
?>

Output: Hello Peter!-1

str_split() Function

Using str_split() Function you can split a string to array. str_split() function accepts 2 parameters string and length. The first parameter sting is the string which you want to split in to arrays. The second parameter length is an optional parameter. By default this is 1. To know how to implement str_split() Function Look at the example below.

<?php
print_r(str_split("biswabhusan",3));
?>

Output: Array ( [0] => bis [1] => wab [2] => hus [3] => an )

strpos() Function

During string operation sometime we required to know the position of a particular word or character in a complete string. In such case strpos() is useful. Using strpos() you can easily know the first occurrence position of any word or character in a complete sentence. Keep remember strpos() function is case-sensitive and binary-safe. It accepts 3 parameters string, find & start. The first parameter string is nothing but the sentence where you want to know the position of a specific word or character. The second parameter Find is the word or character you want to search. The third parameter start is optional. It specifies in the complete string from where after you want to search the find word or character. For an example in below I am with a string “Ravi is a good person.”. Here I want to know the position of “good”.

<?php
echo strpos("Ravi is a good person.","good");
?>

Output: 10

Related Functions

strrpos() – This function helps to find position of the last occurrence of a word or character inside a string. This is case-sensitive.

stripos() – This function helps to find position of the first occurrence of a word or character inside a string. This is case-sensitive.

strripos() – This function finds the position of the last occurrence of a word or character inside a string. This is case-insensitive.

strlen() Function

To know the length of a String we use strlen() function in PHP. This function accepts 1 parameter as string. strlen() always returns integer value. In-case you pass a empty string it return 0. Look at the example below here to strlen I am passing a string “How are you?”. In result strlen() returns 12. It’s means that strlen() function calculates spaces including characters.

<?php
echo strlen("How are you?");
?>

Output: 12

strrev() Function

You must remember those days while to reverse a string we do implement a for loop over the length of a string. Then using echo we print each characters from last to first. In latest revision of PHP (Version 4 onwards) this issue is resolved with strrev() function. Using this function you don’t required to write a complex logic for reverse a string. Simply by passing the string as a parameter to strrev() function you can easily achieve a reverse string. Look at the example below.

<?php
echo strrev("This is an Example.");
?>

Output: .elpmaxE na si sihT

substr() Function

This is a very useful string function. During we extract a part of string from the complete string this function is helpful. substr() function accepts 3 parameters as string, start and length. The first parameter string is nothing but the complete string from which you want to derive part of string. The second parameter start specifies where to start from the complete string. It accepts both negative and positive values. By passing positive number substr() function with start at a specified position from the beginning of the string. If you are passing negative number then it start at a specified position starting from the end of the string. Third parameter length is optional. If in a string of 25 characters length you are passing 2nd and 3rd parameter as 5 and 12 then substr() will return the sub-string from the 5th position to 17th position of complete string. Look at the example below.

<?php
echo substr("This is a awesome School.", 5, 12);
?>

Output: is a awesome.

strtolower() & strtoupper() PHP String Functions

These 2 string functions are used to change the case of Characters. Both of these functions accepts 1 parameter as the string. Using strtolower() you can convert a string to lowercase letters. Similarly using strtoupper() function you can convert a string to uppercase letters. Look at the example below.

<?php
echo strtolower("Hello WORLD.");
?>

Output: hello world.

<?php
echo strtoupper("Hello WORLD!");
?>

Output: HELLO WORLD!