PHP String Functions

In PHP, strings are the fundamental data types used to represent text. PHP string functions can help us modify and manage textual data in our web applications. In this post, we’ll look at some of the most often used PHP string functions and their syntax, along with some examples.

addslashes()

If any string contains backslashes(), single quotes(‘), double quotes(“), and NULL. Then this function adds backslashes() in front of it.

Syntax

addslashes(string_variable)

$str = "Ram's brother is very bad man.";
echo addslashes($str);

Answer is:

Ram’s brother is very bad man.

stripslashes()

It is used to remove backslashes from string which is added by addslashes function.

Syntax

stripslashes(string_variable)

$strVal = stripslashes("Ram's brother is very bad man.");

Answer is:

Ram’s brother is very bad man.

echo()

If we want to display anything on browser, then we use echo function. We can display one or more string with echo.

Syntax

echo(string1, string2, …)

$str = "Ram's brother is very bad man.";
echo $str;

Answer is:

Ram’s brother is very bad man.

explode()

If we want to break any string value into array, then we use explode function.

Syntax

explode(separator, string_variable);

$str = "Ram's brother is very bad man.";
$str_split = explode(" ", $str); //here single space is separator
echo "<pre>";
print_r($str_split);
echo "</pre>";

Answer is:

Array
(
[0] => Ram's
[1] => brother
[2] => is
[3] => very
[4] => bad
[5] => man.
)

implode()

If we want to convert array values to string value, then we use implode function.

Syntax

implode(separator, array_variable)

$arrVar = array("Ram", "is", "a", "good", "boy");
$strVal = implode(" ", $arrVar);
echo $strVal;

Answer is:

Ram is a good boy

Function NameDescription
ltrim()It use to remove white-space or any predefine character from left side of string.
rtrim()It use to remove white-space or any predefine character from right side of string.
trim()It use to remove white-space or any predefine character from both(left & right) side of string.
Syntax

ltrim(string_vaiable, charlist)
rtrim(string_vaiable, charlist)
trim(string_vaiable, charlist)

$strVal = "!Welcome To India!";

echo ltrim($strVal, "!");
echo rtrim($strVal, "!");
echo trim($strVal, "!");

Answer is:

Welcome To India!
!Welcome To India
Welcome To India

Also read about Array Functions in PHP

str_replace()

It is used to replace  character or word to some other character or word in a string. It is a case sensitive(means tata & TATA is two different value).

Syntax

str_replace(search_char, replace_char, string_variable)

$strVal = "Welcome to india";
$strVal = str_replace("india", "INDIA", $strVal);
echo $strVal;

Answer is:

Welcome to INDIA

strlen()

It is used to find length of given string.

Syntax

strlen(string_variable)

$strData = strlen("Welcome india");
echo $strData;

Answer is: 13

strtolower()

It is used to convert a string value in lower case.

Syntax

strtolower(string_variable);

$strData = strtolower("WELCOME INDIA");
echo $strData;

Answer is: welcome india

strtoupper()

It is used to convert a string value in upper case.

Syntax

strtoupper(string_variable);

$strData = strtoupper("welcome india");
echo $strData;

Answer is: WELCOME INDIA

substr()

It is used to extract a part of a string value from a specified position.

Syntax

substr(string_variable, start_pos, string_length)

echo substr("Welcome india", 5, 4);

Answer is: me i

For us to manipulate textual data in our programs in an efficient manner, PHP string functions are essential. These functions make it simple to accomplish operations like formatting, searching, and string manipulation. In order to avoid security flaws like XSS attacks, we need also constantly verify and sanitize user inputs.

Categorized in: