Sunday, August 9, 2015

Some cool String functions in PHP

In this post I am going to discuss some of the cool String functions available in PHP.

Here they go:

1. strlen: Returns the length of the String.
2. str_word_count: Returns the number of words in the given string.
3. strrev: Reverses the string.
4. str_replace: Replaces the String with another value.
5. strpos: Returns the position of the String.
6. bin2hex: Returns the Hexadecimal of the given value.
7. hex2bin: Yes, this does the opposite of Number 6.
8. str_shuffle: This function shuffles the letters of the String.
9. convert_uuencode: Encodes the given String using uuencode encoding.
10. convert_uudecode: This does the opposite of Number 9.
11. str_rot13: This returns the encoding of a given String in ROT 13 encoding. The function also decodes the same String which we gave for encoding.
Let's say we are giving String "Apple", it converts the String as "Nccyr", I hope some of you might have got it, Add 13 to the letter and you convert. A is 1 and 1 + 13 is 14 which is N, following the numbering of letters of 26 English alphabets.
12. crc32: This function calculates and returns 32 bit CRC for a given String.

Here goes the syntax for the above functions:



 <html>  
 <body>  
 <?php  
 function formatHere($a) {  
      echo ("<p style='font-size:20px;'>");  
      echo $a;  
      echo ("</p>");  
 }  
 echo ("<center>");  
 formatHere(strlen("Length of this sentence"));  
 formatHere(str_word_count ("Count The Words Of This Sentence"));  
 formatHere(strrev ("Reverse This"));  
 formatHere(str_replace("replace", "replaced", "String replace"));  
 formatHere(strpos("String Position", "Position"));  
 $a = bin2hex("Convert this");  
 formatHere($a);  
 formatHere(hex2bin($a));  
 formatHere(str_shuffle("Shuffle this"));  
 $b = convert_uuencode("Encode this");  
 formatHere($b);  
 formatHere(convert_uudecode($b));  
 $c = str_rot13("Apple");  
 formatHere($c);  
 formatHere(str_rot13($c));  
 formatHere(crc32("32 bit CRC for this"));  
 echo ("</center>");  
 ?>  
 </body>  
 </html>  


Results returned by String functions in the above code:




Please feel free to drop your suggestions.

No comments:

Comments

blog comments powered by Disqus