ram
ram

Reputation: 613

Convert french to english when upload as a csv file in php

I am using this method for convert French to English. Its working well when I get french characters from my html form. But I've to upload csv file which contains some french characters. It didn't convert while upload csv files.

This is my code while uploading csv

function UploadQuiz(){
   $row =& $this->getTable('quiz');
   $fileName='uploadFile';
   $fileName=$_FILES['uploadFile']['tmp_name'];
   $f = fopen($fileName,"r");

   $success_counter=0; $failure_counter=0;
   while ( ($data = fgetcsv($f, 10000, ",") ) !== FALSE ){
     //perform string escapes so data is entered into database correctly
     $quiz_question = $data[0];
     $option_a = $data[1];
     $option_b = $data[2];
     $option_c = $data[3];
     $option_d = $data[4];
     $option_e = $data[5];
     $quiz_answer = $data[6];
     $quiz_explanation = $data[7];
     $quiz_difficulty = $data[8];
     $chapter_id = $data[9];

     $chapter=$this->check_chapter_id_exist($this->_data['course_id'],$chapter_id);
     if(!$chapter->chapter_id){
       $failure_counter++;
       continue;
     }

     $data['quiz_id'] = '';
     $data['course_id'] = $this->_data['course_id'];
     $data['quiz_question'] = $this->_convertgsm->convertgsm($quiz_question);
     $data['quiz_optionA'] = $this->_convertgsm->convertgsm($option_a);
     $data['quiz_optionB'] = $this->_convertgsm->convertgsm($option_b);
     $data['quiz_optionC'] = $this->_convertgsm->convertgsm($option_c);
     $data['quiz_optionD'] = $this->_convertgsm->convertgsm($option_d);
     $data['quiz_optionE'] = $this->_convertgsm->convertgsm($option_e);

  return $data;
}

Here my method in my php file:

function convertgsm($data){
    $normalizeChars = array(
    '`'=>"'",'Ë'=>'E','Â'=>'A','À'=>'A','Í'=>'I', 'Ì'=>'I', 'Ã'=>'A', 'Î'=>'I',    'Ä'=>'A','Ç'=>'C','Ò'=>'O','Ï'=>'I','È'=>'E','Ó'=>'O','É'=>'E','Ô'=>'O','Ê'=>'E','Õ'=>'O','Ö'=>'O','ê'=>'e','Ù'=>'U','ë'=>'e','Ú'=>'U','ë'=>'e','Ú'=>'U','î'=>'i','Û'=>'U','Û'=>'U','Ü'=>'U','ô'=>'o','Ý'=>'Y','õ'=>'o','â'=>'a','û'=>'u','ã'=>'a','ÿ'=>'y','ç'=>'c','ï'=>'i'    
    );

    $result_data =  strtr($data, $normalizeChars);
    return $result_data;
  }

Could you help me to convert french to english while uploading csv files?

Upvotes: 0

Views: 1045

Answers (1)

Baba
Baba

Reputation: 95131

Usage 1

$translate = new TranslateCSV ();
$str = 'who will win à wôrld cup ?';
var_dump ( $translate->customStrtr ( $str ) );

Output

 string 'who will win a world cup ?' (length=26)

Usage 2

$translate = new TranslateCSV ();
$translate->process ( "PATH_TO_UPLOADED CSV" );

if (! empty ( $translate->getErrors () )) {
    print_r ( $translate->getErrors () );
} else {
    echo "CSV Transalation Completed";
}

Class

class TranslateCSV {
    private $chars = array (
            '`' => "'",
            'Ë' => 'E',
            'Â' => 'A',
            'À' => 'A',
            'Í' => 'I',
            'Ì' => 'I',
            'Ã' => 'A',
            'Î' => 'I',
            'Ä' => 'A',
            'Ç' => 'C',
            'Ò' => 'O',
            'Ï' => 'I',
            'È' => 'E',
            'Ó' => 'O',
            'É' => 'E',
            'Ô' => 'O',
            'Ê' => 'E',
            'Õ' => 'O',
            'Ö' => 'O',
            'ê' => 'e',
            'Ù' => 'U',
            'ë' => 'e',
            'Ú' => 'U',
            'ë' => 'e',
            'Ú' => 'U',
            'î' => 'i',
            'Û' => 'U',
            'Û' => 'U',
            'Ü' => 'U',
            'ô' => 'o',
            'Ý' => 'Y',
            'õ' => 'o',
            'â' => 'a',
            'û' => 'u',
            'ã' => 'a',
            'à' => 'a',
            'ÿ' => 'y',
            'ç' => 'c',
            'ï' => 'i' 
    );

    private $output = array ();
    private $errors = array ();
    private $charsKeys = array ();
    private $charsValues = array ();
    function setChars($chars) {
        $this->chars = $chars;
    }

    function getErrors() {
        return $this->errors;
    }

    function customStrtr($str) {

        if (empty ( $this->charsKeys )) {
            $this->charsKeys = array_keys ( $this->chars );
        }

        if (empty ( $this->charsValues )) {
            $this->charsValues = array_values ( $this->chars );
        }

        $str = str_replace ( $this->charsKeys, $this->charsValues, $str );
        $str = iconv ( 'UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $str );
        return $str;
    }

    function process($csvFile, $csvOut = "") {
        if (! is_file ( $csvFile )) {
            $this->errors [] = "CSV  files does not exist";
            return false;
        }

        if (! empty ( $csvOut ) && ! @touch ( $csvOut )) {
            $this->errors [] = "Can't Create Output CSV";
            return false;
        }

        $fpRead = @fopen ( $csvFile, "r" );

        if (! empty ( $csvOut )) {
            $fpWrite = fopen ( $csvOut, 'w' );
        }

        while ( ($data = fgetcsv ( $fpRead, 1000, "," )) !== FALSE ) {
            $num = count ( $data );
            $field = array ();
            for($c = 0; $c < $num; $c ++) {
                $field [] = $this->customStrtr ( $data [$c] );
            }

            if (! empty ( $csvOut )) {
                fputcsv ( $fpWrite, $field );
            }
        }
        fclose ( $fpRead );

        if (! empty ( $csvOut )) {
            fclose ( $fpWrite );
        }
        return true;
    }
}

Upvotes: 1

Related Questions