How to convert .CSV file to .SRT using PHP

Hello friends to i have assign a new task while working on my current project in which i have to convert a .CSV file into .SRT using PHP . After spending three to four hours i have successfully created a script threw this i can able to convert .CSV file into .SRT very easily. So i decided to share my this script with all of my users. Below is the script -

Script To Convert .CSV To .SRT :

class fileConverter{
 
 public function __construct( $dir )
 {
  //Directory Settings Start Here.
  self::directoryScanner($dir);
 }
  
 public function showMessage($msg = '')
 {
  echo PHP_EOL;
  echo PHP_EOL;
  echo $msg;
 }
  
 public function directoryScanner($dir)
 {
  if (isset($dir) && $dir != '' && is_dir($dir)) {
   // Open a directory, and read its contents
   $files = glob($dir."/*.csv");
   $this->showMessage("Start Reading Directory..");
   foreach($files as $fileno=>$filepath) {
    $this->showMessage("Start reading files from directory..");
    if ($handle = fopen($filepath, "r")) {
     $fileInfo = pathinfo($filepath);
     $myfile = fopen($dir.'/'.$fileInfo['filename'].'.srt', "w");
     $csvData = file_get_contents($filepath, FILE_USE_INCLUDE_PATH);
     $lines = explode("\n", $csvData); 
     foreach ($lines as $count=>$line) {
      $lineData = explode(",", $line, 3);
      fwrite($myfile, $count+1 . PHP_EOL);
      foreach ($lineData as $key=>$data) {
       $data = trim(str_replace('"',"", $data));       
       if($key!=2){
        $index = strrpos( $data, ':' ); 
        if( $index !== FALSE )
        $data[ $index ] = ',';
        if($key ==0){
         $data .= " --> ";
        }else{
         $data .= PHP_EOL;
        }
       }else{
        $data .= PHP_EOL;
        $data .= PHP_EOL;
       }
       
       fwrite($myfile, $data);
      }
     }
    }
    fclose($myfile);
    $this->showMessage($fileno+1 ."-File conversion completed successfully..");
   }
  } else {
   echo "Folder not found" . PHP_EOL;
  }
  
 }
}

//Creating Instance of fileConverter class
$ptr = new fileConverter($argv[1]);


Method Of Uses :

You have to copy and the above code in PHP file and save Save it any location in you system. In my case i saved this on my Desktop.
 
Now you have to put all of your .CSV files into a folder and run the above script.


In the above picture as you can see that i am putting my all .CSV file into the  folder name "files" and this folder are on Desktop. So now i am running the below command from command prompt -



 You can see that i have copying the above script and save it on Desktop with the name fileConverter.php. Then i go to the Desktop location and run the above command that you seeing in the screen shot.

 

Now you can see that all of my seven .CSV files have been successfully converted into the .SRT file.

Note : You must be installed the PHP on your system before running this command.

Output : In the below picture now you can see that all of my .CSV files has been successfully converted into .SRT file.

 


Please don't forgot to leave a comment if you like this post. 

Chears :)
Happy Coding..

Post a Comment

Previous Post Next Post