String Function of PHP
String Function of PHP
chr()
The chr() function returns a character from the
specified ASCII value.
Syntax:
chr(ascii)
Example
<?php
echo chr(53)."<br />";
?>
Output:
5
A
d
ord()
The ord() function returns the ASCII value of
the first character of a string.
Syntax:
ord(string)
Example
<?php
echo ord("atmiya")."<br />";
echo ord("a")."<br />";
?>
Output:
97
97
strtolower()
Syntax:
strtolower(string);
Example
<?php
echo strtolower("Hello BCA 3 Sem");
?>
Output:
hello bca 3 sem
strtoupper()
This function converts the string to upper case.
Syntax:
strtoupper(<string>);
Example
<?php
echo strtoupper("Hello BCA 3 Sem");
?>
Output:
HELLO BCA 3 SEM
strlen()
This function returns the length of the string.
Syntax:
strlen(<string>);
Example
echo strlen(Atmiya);
$a=str;
echo strlen($a);
Output:
6
3
ltrim()
The ltrim() function is used to remove white spaces or other predefined characters form the left side of the string.
ltrim(string_name, char_list)
Example
<?php
$text1=" Hello BCA 3 Sem";
$text2="\t\n Hello BCA 3 Sem";
echo ltrim($text1)."<br>";
echo ltrim($text2);
?>
Output:
Hello BCA 3 Sem
Hello BCA 3 Sem
rtrim()
The rtrim() function is used to remove
the whitespaces and other predefined characters
from the right side of a string.
Syntax:
rtrim(string_name, char_list)
Example
<?php
$text1="Hello BCA 3 Sem ";
$text2="\t\n Hello BCA 3 Sem";
echo rtrim($text1)."<br>";
echo rtrim($text2,'em');
?>
Output:
Hello BCA 3 Sem
Hello BCA 3 S
trim()
The trim() function is used to remove the white
spaces and other predefined characters from the left
and right sides of a string.
Syntax:
trim(string_name, char_list)
Example
<?php
$str=" \tWelcome to BCA 3 Sem ";
$str1=" Welcome to BCA 3 Sem ";
echo trim($str)."<br>";
echo trim($str1);
?>
substr()
The substr() function used to cut a part of a string
from a string, starting at a specified position.
Syntax:
substr(string_name, start_pos, length_to_cut)
Example
<?php
$str="Welcome to BCA 3 Sem";
echo substr($str,0,5)."<br>";
echo substr($str,5);
?>
Welco
me to BCA 3 Sem
strcmp()
The strcmp() function is used to compare two strings.
Syntax:
strcmp(string1, string2)
Example
<?php
$str="Welcome to BCA 3 Sem";
strcasecmp()
The strcasecmp() function is used to compare two case
sensitive strings.
Syntax:
strcasecmp(string1, string2)
This function returns:
Example
<?php
$str="Atmiya";
$str1="ATMIYA";
echo $result=strcasecmp($str,$str1)."<br>";
$str2="ATMIYA";
$str3="ATMIYA RJT";
echo $result=strcmp($str2,$str3)."<br>";
echo $result=strcmp($str3,$str2)."<br>";
?> 0 -4 4
strpos()
The strpos() function returns the position of the first
occurrence of a string inside another string.
Syntax:
strpos(string,find,start)
Example
<?php
$str="Aatmiya";
$str1="ATMIYA";
echo strpos($str,"a",3)."<br>";
echo strpos($str1,"b");
?>
6
strrpos()
The strrpos() function finds the position of the last
occurrence of a string inside another string.
Syntax:
strrpos(string,find,start)
Example
<?php
$str="Aatmiy";
$str1="ATMIYATA";
echo strrpos($str,"at")."<br>";
echo strrpos($str1,"A",6);
?>
1
strstr()
The strstr() function searches for the first occurrence
of a string inside another string.
This function returns the rest of the string (from the
matching point).
Example
<?php
$str="Aatmiy";
echo strstr($str,"at);
?>
atmiy
stristr()
The stristr() function searches for the first occurrence
of a string inside another string.
Syntax:
stristr(string,search)
Example
<?php
$str="Aatmiy";
echo stristr($str,"At")."<br>";
?>
atmiy
str_replace()
The str_replace() function replaces some characters
with some other characters in a string.
Syntax:
Example
<?php
$str="Atmiya College";
echo str_replace("College","Rajkot",$str)."<br>";
str_replace("College","Rajkot",$str,$i);
echo "Replacements=".$i;
$arr=array("one","two","three");
$arr1=str_replace("three","four",$arr);
print_r($arr1);
?>Atmiya Rajkot
Replacements=1
Array ( [0] => one [1] => two [2] => four )
strrev()
The strrev() function is used to reverse a string.
Syntax:
strrev(main_string)
Example
<?php
$str="Atmiya College";
echo strrev($str);
?>
egelloC ayimtA
Questions?