String Functions in PHP

String functions allow you to manipulate string in various ways. You can perform different operations on string using these functions.
String functions are explained in detail below:

(1) chr:

chr () function accepts ASCII value as an argument and returns a character value corresponds to the ASCII value specified as an argument.
Syntax:
chr (ASCII-Value)

<? php
echo chr (65);
?>
Output:
A


(2) ord:

ord () function accepts a string as an argument and returns the ASCII value of the first character of a string specified as an argument.
Syntax:
ord (string)

<?php
echo ord(“Y”).”<br/>”;
echo ord(“Yesha”).”<br/>”;
echo ord(“yesha”).”<br/>”;
?>
Output:
89
89
121

     

(3) strtolower:

strtolower () function accepts a string as an argument and converts all the characters of the specified string into lowercase letters.
If the characters in the string are already in the lowercase letters then they remains as it is.
Syntax:
strtolower (string)

<?php
echo strtolower(“yesha”).”<br/>”;
echo strtolower (“Yesha”).”<br/>”;
echo strtolower (“YESHA”).”<br/>”;
?>
Output:
yesha
yesha
yesha


(4) strtoupper:

strtoupper () function accepts a string as an argument and converts all the characters of the specified string into uppercase letters.
If the characters in the string are already in the uppercase letters then they remains as it is.
Syntax:
strtoupper (string)

<?php
echo strtoupper(“yesha”).”<br/>”;
echo strtoupper (“Yesha”).”<br/>”;
echo strtoupper (“YESHA”).”<br/>”;
?>
Output:
YESHA
YESHA
YESHA

(5) strlen:

strlen () function accepts a string as an argument and returns an integer value indicating the length of a string specified as an argument.
Length of the string means total number of characters in the string.
Syntax:
strlen (string)

<?php
echo strlen(“Yesha”).”<br/>”;
echo strlen (“Yesha Oza”).”<br/>”;
?>
Output:
5
9


(6) ltrim:

ltrim () function accepts a string as an argument and removes white spaces from the left side of the string specified as an argument.
You can also use ltrim () function to remove several predefined white space characters from the left side of the string specified as an argument.
Syntax:
ltrim (string [, predefine character])
Here,
string
indicates the string from which you want to remove white spaces from left side.
Predefined character indicates the particular white space to be removed from the left side of the string. It is an optional argument. If not specified then the ltrim () removes all the white space characters from the left side of the string. It can have one of the following values:

Symbol Meaning
“” Normal Space
“\0” Null character
“\t” Tab Space
“\n” New Line Character
“\r” Carriage Return

<?php
$name = "\tYesha";
echo $name."<br/>";
echo ltrim($name);
?>
Output:
Yesha
Yesha
If you view the output in the browser no effect will be seen but if you right click in the browser and click view source then it will looks like as shown below:
Yesha
Yesha


(7) rtrim :

rtrim () function accepts a string as an argument and removes white spaces from the right side of the string specified as an argument.
You can also use rtrim () function to remove several predefined white space characters from the right side of the string specified as an argument.
Syntax:
rtrim (string [, predefine character])
Here,
string
indicates the string from which you want to remove white spaces from right side.
Predefined character indicates the particular white space to be removed from the right side of the string. It is an optional argument. If not specified then the rtrim () removes all the white space characters from the right side of the string. It can have one of the following values:

Symbol Meaning
“” Normal Space
“\0” Null character
“\t” Tab Space
“\n” New Line Character
“\r” Carriage Return

<?php
$name = "\tYesha\n";
echo $name."<br/>";
echo rtrim($name);
?>
Output:
Yesha
Yesha
If you view the output in the browser no effect will be seen but if you right click in the browser and click view source then it will looks like as shown below:
Yesha
Yesha


(8) trim:

trim () function accepts a string as an argument and removes white spaces from both side of the string specified as an argument.
You can also use trim () function to remove several predefined white space characters from both side of the string specified as an argument.
Syntax:
trim (string [, predefine character])
Here,
string
indicates the string from which you want to remove white spaces from both side.
Predefined character indicates the particular white space to be removed from both side of the string. It is an optional argument. If not specified then the trim () removes all the white space characters from both side of the string. It can have one of the following values:

Symbol Meaning
“” Normal Space
“\0” Null character
“\t” Tab Space
“\n” New Line Character
“\r” Carriage Return

<?php
$name = "\tYesha\n";
echo $name."<br/>";
echo trim($name);
?>
Output:
Yesha
Yesha
If you view the output in the browser no effect will be seen but if you right click in the browser and click view source then it will looks like as shown below:
Yesha
Yesha


(9) substr:

substr () function accepts string as an argument and returns specific part of the string.
Syntax:
substr (string, st [, number])
Here,
String
is a string from which you want to retrieve specific part of the string.
St is the position within string from which you want to retrieve specific characters. It can have one of the following three types of value:
0 – indicates starting from the first character of the string
Positive – Positive Number indicates starting from specified position from the beginning of the string.
Negative – Negative Number indicates starting from specified position from the end of the string.
Number indicates number of characters to be retrieved from specified position. It is an optional argument. If not specified then substr () returns all the characters from specified position. It can have one of the following two values:
Positive – Indicates number of characters to be returned from the starting position specified.
Negative – Indicates Number of characters to be returned from the end of the string.

<?php
$a = "Hello Welcome To The World of PHP";
echo substr($a,0,5)."</br>";
echo substr($a,-3)."</br>";
echo substr($a,6)."</br>";
echo substr($a,-6)."</br>";
echo substr($a,6,27)."</br>";
?>
Output:
Hello
PHP
Welcome To The World of PHP
of PHP
Welcome To The World of PHP


(10) strcmp:

strcmp () function accepts two string as an argument and compare that strings. It returns one of the following three values:
• 0 - if the two strings are equal
• <0 - if string1 is less than string2
• >0 - if string1 is greater than string2
The strcmp () function performs case sensitive comparison of strings.
Syntax:
Strcmp (String1, String2)

<?php
$a="HELLO";
$b="hello";
if (strcmp($a,$b)==0)
echo "String are equal";
else if (strcmp($a,$b)<0)
echo "a is less then b";
else if(strcmp($a,$b)>0)
echo "a is greater then b";
?>
Output:
a is less then b


(11) strcasecmp:

strcasecmp () function accepts two string as an argument and compare that strings. It returns one of the following three values:
• 0 - if the two strings are equal
• <0 - if string1 is less than string2
• >0 - if string1 is greater than string2
The strcasecmp () function performs case insensitive comparison of strings.
Syntax:
Strcasecmp (String1, String2)

<?php
$a="HELLO";
$b="hello";
if (strcasecmp($a,$b)==0)
echo "String are equal";
else if (strcasecmp($a,$b)<0)
echo "a is less then b";
else if(strcasecmp($a,$b)>0)
echo "a is greater then b";
?>
Output:
String are equal


(12) strrpos:

strpos () function accepts two strings as an argument and returns the position of the first occurrence of a search string inside another string.
If the search string is not found in the original string then it returns False.
You can also specify the starting position in the original string from which you want to perform search. However it is optional. If not specify then it will start searching from the first character of the original string.
strpos () function performs case sensitive search.
Syntax:
Strpos (OriginalString, SearchString [, Start])

<?php
echo strpos("Hello How Are You","How")."<br/>";
echo strpos("Hello How Are You","how")."<br/>";
?>
Output: 6


(13) strstr:

strstr () function accepts two strings as an argument and searches for the first occurrence of a search string inside original string.
If search string is found within the original string then it returns the rest of the string from the matching position. If search string is not found within the original string then it returns False.
Strstr () function performs case sensitive search.
Syntax:
strstr (OriginalString, SearchString)

<? php
echo strstr("Hello How Are You","How")."<br/>";
echo strstr("Hello How Are You","how")."<br/>";
?>
Output:
How Are You


(14) stristr:

stristr () function accepts two strings as an argument and searches for the first occurrence of a search string inside original string.
If search string is found within the original string then it returns the rest of the string from the matching position. If search string is not found within the original string then it returns False.
stristr () function performs case insensitive search.
Syntax:
stristr (OriginalString, SearchString)

<? php
echo stristr("Hello How Are You","How")."<br/>";
echo stristr("Hello How Are You","how")."<br/>";
?>
Output:
How Are You
How Are You

(15) str_replace:

str_replace () function accepts a string as an argument and replaces specified characters of the string with another specified characters.
It returns a string after replacing specified characters with other characters.
It also returns an integer value indicating number of replacement occurs in the specified string.
It is case sensitive. If you want to perform case insensitive find and replacement in the string then use str_ireplace () function.
Syntax:
str_replace (Find-Character, Replace-Character, String, Occurrence)
Here,
Find-Character
indicates the character to be find in the original string.
Replace-Character indicates the character to be replaced in place of the Find - Character.
String indicates the original string in which you want to find characters and then replace characters.
Occurrence is an optional argument. It indicates number of replacement made in the original string.
Consider Following things:
(1) If the string to be searched is an array, it returns an array.
(2) If the string to be searched is an array, find and replace is performed with every array element.
(3) If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace.
(4) If find is an array and replace is a string, the replace string will be used for every find value.

<?php
$a = "Good Morning";
echo str_replace("Morning","Night",$a,$i);
?>
Output:
Good Morning
Good Night
Example:
<?php
$Team = array("India","Australia","England","NewZealand");
print_r(str_replace("England","West Indies",$Team,$i));
?>
Output: Array ([0] => India [1] => Australia [2] => West Indies [3]=>NewZealand )


(16) strrev:

strrev () function accepts a string as an argument and reverses that string.
Syntax:
strrev (string)

<? php
$a="HELLO";
echo strrev ($a); ?>
Output:
OLLEH


(17) echo:

echo () function accepts one or more strings as an argument and display one or more strings on the browser.
Syntax:
echo String

<?php
$name="Yesha";
echo "Hello","How","Are","You";
echo "Hello", "<br/>","I am Fine","<br/>";
echo "My Name is $name";
?>
Output:
HelloHowAreYouHello
I am Fine
My Name is Yesha


(18) print:

print () function accepts one or more strings as an argument and display one or more strings on browser.
Syntax:
print string

<?php
$name="Yesha";
print "Hello"."How"."Are"."You";
print "Hello". "<br/>"."I am Fine"."<br/>";
print "My Name is $name";
?>
Output:
HelloHowAreYouHello
I am Fine
My Name is Yesha

Note: The difference between echo and print functions is that echo function is faster then print function.

Download Projects


Download Programs