Breaking

Sunday, March 1, 2020

php tutorial , How to use php string function ?

Here is php tutorial about how to use string function in php, now lets start our tutorial




substr( ) : return sub string of main string 

<?php

$string = "hello world how are you";
echo substr($string, 5, 6 );


?>

result : world

strlen() : return length of string 

<?php

$string = "hello world how are you";
echo strlen($string);


?>

result : 23

echo() : print message 

<?php

echo "hello world";


?>

result : hello world

strpos() : return perticuler word position in string 

<?php 

$string = "hello world how are you";
echo  strpos($string,'are',3);

?>

result : 16

implode() : convert array to string, It will very usefull

<?php 

$name = array('a', 'b', 'c', 'd');
$string = implode('-', $name);
echo $string;
?>

result : a-b-c-d

explode() : convert string to array, it break string to specific delimiter

<?php 

$string = "hello world how are you";
$result =  explode(" ",$string);
print_r($result);
?>
result : Array ( [0] => hello [1] => world [2] => how [3] => are [4] => you )

print_r() : print array 

above example 

str_replace() : It find word and replace it by specific string

<?php 
$string = "hello world , how are you";
$search = "world";
$replace = "jhon";
echo  str_replace ( $search, $replace, $string );
?>

result : hello jhon , how are you

strtolower() : convert string to lower case 

<?php 
$string = "HELLO WORLD, HOW ARE YOU";
echo  strtolower($string );
?>

result : hello world, how are you

strtoupper() : convert lower string to upper case

<?php 
$string = "hello world, how are you";
echo  strtoupper($string );
?>
result : HELLO WORLD, HOW ARE YOU

str_repeat() : repeat the string 

<?php 
$string = "hello world, how are you";
echo  str_repeat($string,3);
?>
result : hello world, how are youhello world, how are youhello world, how are you

str_pad() : hide some character, use full like in check, bank account number 

<?php
echo "account nu is :";
echo str_pad('5689296', 20, '#');
?> 

result : account nu is :5689296#############

trim() ltrim(), rtrim() : It remove white spaces and unwanted string from both side of string 

<?php
$string = 'hello world how          are you hello';
$trimstring = trim($string, 'hello');
echo $trimstring;
?>
result : world how are you

<?php
$string = 'hello world how          are you hello';
$trimstring = rtrim($string, 'hello');
echo $trimstring;
?>

result : hello world how are you

<?php
$string = 'hello world how          are you hello';
$trimstring = ltrim($string, 'hello');
echo $trimstring;
?>

result : world how are you hello

str_split() : convert string to array 

<?php
$string = 'HELLO';
$result = str_split($string);
  print_r($result);
?>

result : Array ( [0] => H [1] => E [2] => L [3] => L [4] => O )

md5() , sha1(): useful when you want to encrypt string 

<?php
$string = 'HELLO';
echo 'md5:==' . md5($string). '<br>';
echo 'sha1:==' . sha1($string).'<br>';
?>
result : 
md5:==eb61eead90e3b899c6bcbe27ac581660
sha1:==c65f99f8c5376adadddc46d5cbcf5762f9e55eb7

base64_encode(), base64_decode :  it will useful when you want to pass parameter in url with encrypt and dycrypt 
<?php
$string = 'HELLO';
$id = base64_encode($string);
echo "encrypt --". $id;
echo "dycrypt --" . base64_decode($id );
?>
result : encrypt --SEVMTE8=dcrypipt --HELLO

strip_tags()  : function strips a string from HTML tags.

<?php
$stringwithtag = '<h2>hello world<p>how are you</p><small>this is small text</small></h2>';
$withouttag = strip_tags($stringwithtag);
echo $withouttag;
echo 'with tag' . $stringwithtag ;
?>
result : 
hello worldhow are youthis is small textwith tag

hello worldhow are you
this is small text


strcmp() definition:  function compares two strings.


<?php
echo strcmp("jhon", "jhon"); 

?>
result : 0 means true

strrev() : print string in reverse order 

<?php
echo  strrev("hello world"); 

?>
result : dlrow olleh




No comments:

Post a Comment

your suggestion are welcome by me