Wednesday, 6 April 2016

Important String Function in PHP

trim() Function
Removes whitespace or other characters from both sides of a string
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
ltrim() function

Removes whitespace or other characters from the left side of a string.
<?php
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>

rtrim() function

Removes whitespace or other characters from the right side of a string.
<?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");

?>

strtolower()

Converts a string to lowercase letters
<?php
echo strtolower("Hello WORLD.");
?>

strtoupper()

Converts a string to uppercase letters
<?php
echo strtoupper("Hello WORLD!");

?>

substr() function

Returns a part of a string
<?php
echo substr("Hello world",6);
?>

O/P-world 

substr_replace() function

Replaces a part of a string with another string.
<?php
echo substr_replace("Hello","world",0); // 0 will start replacing at the first character in the string

?>

sha1() function

Calculates the SHA-1 hash of a string
<?php
$str = "Hello";
echo sha1($str);
?>

md5() function

Calculates the MD5 hash of a string
<?php
$str = "Hello";
echo md5($str);
?>

strlen() function

Returns the length of a string
<?php
echo strlen("Hello");
?>

strpos() function

The strpos() function finds the position of the first occurrence of a string inside another string.
<?php
echo strpos("I love php, I love php too!","php");

?>  

0 comments:

Post a Comment