0

我正在使用这种方法将法语转换为英语。当我从我的 html 表单中获取法语字符时,它运行良好。但我必须上传包含一些法语字符的 csv 文件。上传 csv 文件时它没有转换。

这是我上传 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;
}

这是我的php文件中的方法:

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;
  }

你能帮我在上传 csv 文件时将法语转换为英语吗?

4

1 回答 1

1

用法一

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

输出

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

用法2

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

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

班级

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;
    }
}
于 2012-04-14T09:10:13.823 回答