PHP String Function
PHP String Function
Example:
<?php
$str="My name is CSE";
$str=strtolower($str);
echo $str;
?>
Output:
my name is cse
Example:
<?php
$str="My name is php";
$str=strtoupper($str);
echo $str;
?>
Output:
MY NAME IS PHP
Example:
<?php
$str="my name is PHP";
$str=ucfirst($str);
echo $str;
?>
Output:
My name is PHP
Example:
<?php
$str="MY name IS PHP";
$str=lcfirst($str);
echo $str;
?>
Output:
mY name IS PHP
Example:
<?php
$str="my name is cse third year";
$str=ucwords($str);
echo $str;
?>
Output:
My Name Is Cse Third Year
Example:
<?php
$str="my name is cse third year";
$str=strrev($str);
echo $str;
?>
Output:
raey driht esc si eman ym
Example:
<?php
$str="my name is cse third year";
$str=strlen($str);
echo $str;
?>
Output:
25
Output:
Your string is :Hello World!
By using 'chop()' Functions is your string is: Hello
Example:
<?php
$str = "Hello CSE3rdYear!";
echo "Your string is:".$str;
echo "</br>";
echo "By using 'chunk_split()' function your string is:".chunk_split($str,6,"...");
?>
Output:
Your string is:Hello CSE3rdYear!
By using 'chunk_split()' function your string is:Hello ...CSE3rd...Year!...
PHP count_chars()function:
PHP count_chars() is most important string function. It is used to return information about
characters in a string.
Note:
3 : a string containing all unique characters is returned.
4 : a string containing all not used characters is returned.
Example-1
<?php
$str = "cse branch!";
echo "Your given string: ".$str;
echo "<br>"."By using 'count_chars()' function your string is :".count_chars($str,3);
?>
Output:
Your given string: cse branch!
By using 'count_chars()' function your string is : !abcehnrs
Example-2
<?php
$str = "cse branch!";
echo "Your given string: ".$str;
echo "<br>"."By using 'count_chars()' function your string is :".count_chars($str,4);
?>
Output:
Your given string: cse branch!
By using 'count_chars()' function your string is :
-"#$%&'()*+,-
./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`dfgijklmopqtuvwxyz{
|}~����
It returns:
o If the two strings are equal: [ 0 ]
o If string1 is less than string2: [< 0]
o If string1 is greater than string2: > 0
Example:
<?php
$str1 = "helloCSE";
$str2 = "HELLOcse";
echo "Your first string is:".$str1;
echo "<br>";
echo "Your second string is:".$str2;
echo "<br>";
echo strcasecmp("$str1","$str2");
?>
Output:
Your first string is:helloCSE
Your second string is:HELLOcse
0
Example:
<?php
$str1="Hello CSE";
$str2="CSE";
echo strchr($str1,$str2);
?>
Output:
CSE
Example:
<?php
$str1="Hello CSE";
$str2="CSE";
echo strchr($str1,"e");
?>
Output:
ello CSE
Example:
<?php
echo strcmp("Hello ", "HELLO"). " because the first string is greater than the second
string.";
echo "</br>";
echo strcmp("Hello cse", "Hello cse Hello"). " because the first string is less than the
second string.";
?>
Output:
8192 because the first string is greater than the second string.
-6 because the first string is less than the second string.
Note:
Hello Hello 4 String1 > String2 because of String1 greater than String2
PHP by 6 characters including whitespace.
Hello Hello -4 String1 < String2 because String1 is lesser than String2 by
PHP 4 characters including whitespace.