String Functions In PHP
String Functions In PHP:
Substr(): substr() is used for collecting part of a
string.
Substr(string,start
index[,length])
<?php
echo
substr("hello world",6,5);
?>
OutPut
: world
Substr_count():
function used for counting occurance of
substring in main string.
Substr_count(string,substring[,string][,length]);
<?php
echo
substr_count("hello world . the word is nice","world");
?>
Output : 1
Str-repeate($string,repeate
count) : repeating a string number of times.
<?php
echo
str_repeat("india",100);
?>
OutPut:
indiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindiaindia
strtolower(string)
:
strtoupper(string)
strcasecmp(string1,string2)
case insensitive string comparison. This
function returns 0 it both the strings are equal otherwise it returns the
numerical difference between the first non matching character(so the return
value can be negative or positive or zero).
Strcmp(string1,
string2) :
Case
sensitive string comparison . this function returns ;
0 - it both the strings are equal.
1 – it the first string
is greaterthan secong.
-1 – second string is
greater than first.
<?php
$s1 = "abc";
$s2 = "aba";
$n = strcasecmp($n,
$s2);
echo $n;
?>
OutPut:
-3
Str_pad() : string pad with specified length.
Str_pad(string,length[,pad_string][,pad_type])
Where the pad type
argument is a predefined constant of php and will be either of the following.
STR_PAD_LEFT
STR_PAD_RIGHT
STR_PAD_BOTH
<?php
$str
="india";
$str = str_pad($str,
20,"*",STR_PAD_BOTH);
echo
strlen($str)."--".$str1."<br>";
echo
STR_PAD_LEFT."--".STR_PAD_BOTH;
?>
OutPut:
20--
0—2
strip_tags()
: removing html
and php tags with in a string.
Strip_tags(string[,allow]);
Where string aruguement specifies the
string to check and allow argument is optional which specifies allowable tags.
These tags will not be removed.
<?php
$str
="<h1>indian</h1><h2>technologies</h2><b>private</b><ins>limited</ins>";
$str1 =
strip_tags($str);
echo
$str1."<br>";
$str2 =
strip_tags($str,"<b><ins>");
echo
$str2."<br>";
?>
OutPut:
indiantechnologiesprivatelimited
indiantechnologiesprivatelimited
ucwords(string)
: converts
the first character of each word in a string to uppercase.
Echo ucwords(“India”);
Strtok(string,delimeter)
: splits a
string into smaller strings. Used for tokenizing a string based on a delimeter.
<?php
$str ="indian
technologies private limited";
$token = strtok($str,
" ");
while($token)
{
echo $token."<br>";
$token = strtok(" ");
}
$data = explode("
", $str);
print_r($data);
?>
OutPut:
indian
technologies
private
limited
Array ( [0] =>
indian [1] => technologies [2] => private [3] => limited )
Strtr(string,from,to):
transulates certain characters in a string.
<?php
echo strtr("hilla
warld", "ia","eo");
?>
<?php
echo
"<br>";
$arr =
array("hello"=>"hi","world"=>"earth");
echo strtr("hello
world",$arr);
?>
OutPut:
hello world
hi earth
str_shuffle(string)
: function
used to shuffle the string in each request.
<?php
$str =
"ABCDEFGHIJK1234567";
$s = str_shuffle($str);
echo
$s."<br>";
echo substr($s,0,5);
?>
OutPut:
3BJE4K5FC21A7DGH6I
3BJE4
Strpos(source
string, search string):
returns the first index of the search string in source string.
<?php
$str = "Indian
Tech";
echo
strpos($str,"d");
echo
"<br>";
echo
strpos($str,"Tech");
?>
OutPut
2
7
Ord(character):
function used to get the ASCII value of a
character.
<?php
echo "ASCII value
of A = ". ord('A');
?>
OutPut:
ASCII value of A = 65
Chr(ASCII
value): function used to get the corresponding
character value of the ASCII specified.
Echo chr(65);
OutPut : A
Very informative and It was an awesome post. I love reading your fantastic content. Thanks for sharing it with us. We are so greatful to your sharing.Rapid PHP 2018
ReplyDelete