15 Important PHP string function for daily Use

Hello friends

here is some Important 15 php function use in daily php work  . 


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

2.  ltrim : Removes whitespace or other characters from the right side of a string

Example :
<?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"Hello");
?>

3.  substr() :    Returns a part of a string

Example :
<?php
echo substr("Hello world",6);
?>

4. trim() :     Removes whitespace or other characters from both sides of a string

Example:
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>

5. ucfirst()     Converts the first character of a string to uppercase
6. ucwords()     Converts the first character of each word in a string to uppercase
7. wordwrap()     Wraps a string to a given number of characters
Example :
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n");
?>

8. money_format() :      Returns a string formatted as a currency string
Example :
<?php
$number = 1234.56;
setlocale(LC_MONETARY,"en_US");
echo money_format("The price is %i", $number);
?>

9.implode() :    Returns a string from the elements of an array
Example :<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>

10. explode() :    Breaks a string into an array

Example :
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>

11. md5() :     Calculates the MD5 hash of a string

Example :
<?php
$str = "Hello";
echo md5($str);
?>

Output : 8b1a9953c4611296a827abf8c47804d7

12. number_format() :    Formats a number with grouped thousands

Example : <?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>

Output :
1,000,000
1,000,000.00
1.000.000,00

13. str_ireplace() :    Replaces some characters in a string (case-insensitive)

Example :<?php
echo str_ireplace("WORLD","Peter","Hello world!");
?>

Output : Hello Peter!


14. str_replace() :     Replaces some characters in a string (case-sensitive)

<?php
echo str_replace("world","Peter","Hello world!");
?>

Output : Hello Peter!


15.  echo() :    Outputs one or more strings

Example : <?php
echo "Hello world!";
?>

Output : Hello world!


Special Thanks to W3schools.com

No comments:

Post a Comment

Pages