Bacis Php String Function

We will look at some commonly used functions to manipulate strings

1.The syntax strlen() function returns the length of a string (number of characters).
e.g
<?php
echo strlen("Hello India!"); // outputs 12
?>
2. The syntax str_replace() function replaces some characters with some other characters in a string
str_replace(find,replace,string,count) 

where
2.1. find :     Specifies the value to find in string.
2.2  replace: The value to replace the value in find.
2.3  string :  The string to be searched.
2.4  count : A variable that counts the number of replacements.

e.g
<?php
echo str_replace("world", "India", "Hello world!"); // outputs Hello India!
?>
3.The  syntax  str_ireplace() function replaces some characters with some other characters in a string.
This function is case-insensitive. Use the  str_replace() function to perform a case-sensitive search
e.g
<?php
echo str_ireplace("WORLD","INDIA","Hello world!"); // outputs Hello INDIA!
?>
4. The syntax implode(separator,array) is a string function to convert array value into string.
e.g 
<?php
$array = array('lastname', 'email', 'phone');
$parller_separated = implode("|", $array);

echo $
parller_separated; //Out put  lastname|email|phone
?> 

5. The md5(string,raw) 
where string is to be calculated.
raw is either hex or binary output format:
         5.1 TRUE - Raw 16 character binary format
         5.2 FALSE - Default. 32 character hex number
e.g
<?php
$str = "india";
echo "The string: ".$str."<br>";
echo "TRUE - Raw 16 character binary format: ".md5($str, TRUE)."<br>";// out put ©ƒtëìŽ zTu !a€M
echo "FALSE - 32 character hex number: ".md5($str); // out put 11a98374ebec8e0c7a54751d2161804d
?>
6. The str_split(string,length) — Convert a string to an array.
 where string is specifies the string to split.
 leanth is specifies the length of each array element. default is 1
e.g
<?php
$str = "Hello Friend";

$arr1 = str_split($str);https://www.blogger.com/blogger.g?blogID=7836795689887315241#editor/target=post;postID=4243752372458883062;onPublishedMenu=allposts;onClosedMenu=allposts;postNum=0;src=link
$arr2 = str_split($str, 3);

print_r($arr1);
echo "<br>";
print_r($arr2);
?> 

7. The strtr(string,from,to) function translates certain characters in a string.
  strtr(string,from,to) or strtr(string,array)
  where string specifies the string to translate.
  from specifies what characters to change.
  to specifies what characters to change into.
  array  containing what to change from as key, and what to change to as value
e.g
<?php
$arr = array("Hello" => "Hello", "world" => "india");
echo strtr("Hello world",$arr);
?>    






Comments