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.

Friday, August 7, 2015

Creation of zip file using Java

I want to show how we can create a Zip file in this post.

I am going to use a classes called ZipEntry and ZipOutputStream which are part of java.util.zip.

ZipEntry is used to create a zip file and ZipOutputStream takes data in an output stream and encloses the files in a zip.

I am reading the data from File Input Stream and writing to Zip Output Stream.

Reading count from file input stream and creating a byte of same size to accommodate the byte with file data and write to zip output stream.

I am writing data to File Output Stream and passing the File Output Stream instance variable as parameter to Zip Output Stream for the zip file to be created.

After writing data to zip file, we have to close the zip output stream and file output stream.

Check the below code for creating zip files using Java.


 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.util.zip.ZipEntry;  
 import java.util.zip.ZipOutputStream;  
 public class TestClass {  
      /**  
       * @param args  
       * @throws IOException  
       */  
      public static void main(String[] args) throws IOException {  
           String textFileName = "F:/abc.txt";  
           String zipFileName = "F:/createdFile.zip";  
           File textFile = new File(textFileName);  
           File zipFile = new File(zipFileName);  
           FileInputStream fiStream = new FileInputStream(textFile);  
           FileOutputStream foStream = new FileOutputStream(zipFile);  
           int count = fiStream.read();  
           byte[] bytes = new byte[count];  
           ZipEntry zipEntry = new ZipEntry(textFileName);  
           if (!zipFile.exists()) {  
                zipFile.createNewFile();  
           }  
           ZipOutputStream zoStream = new ZipOutputStream(foStream);  
           zos.putNextEntry(zipEntry);  
           while ((count = fis.read(bytes)) >= 0) {  
                zoStream.write(bytes, 0, count);  
           }  
           fiStream.close();  
           zoStream.closeEntry();  
           zoStream.close();  
           foStream.close();  
      }  
 }  

Comments

blog comments powered by Disqus