Question

Cette fonction fait partie du plugin projectmanager dans wordpress. Je suis en train d'importer un fichier csv, mais ce fichier csv contient des accents becasue il est fait des noms tels que « Adrián » ou « Damián ». Mais lorsque ce code passe ces noms, il supprime tout de la lettre avec un accent laissant « Adri » et « Dami ».

Y at-il ici je pourrais changer d'éliminer ce bug?

function importDatasets( $project_id, $file, $delimiter, $cols )
{
    if ( !current_user_can('import_datasets') ) {
        $this->setMessage( __("You don't have permission to perform this task", 'projectmanager'), true );
        return;
    }


    if ( $file['size'] > 0 ) {
        /*
        * Upload CSV file to image directory, temporarily
        */
        $new_file =  parent::getFilePath().'/'.basename($file['name']);
        if ( move_uploaded_file($file['tmp_name'], $new_file) ) {
            $handle = @fopen($new_file, "r");
            if ($handle) {
                if ( "TAB" == $delimiter ) $delimiter = "\t"; // correct tabular delimiter

                $i = 0; $l=0; // initialize dataset & line counter
                while (!feof($handle)) {
                      $buffer = fgets($handle, 4096);
                      $line = explode($delimiter, $buffer);

                      if ( $l > 0 && $line ) {
                      $name = $line[0];
                      $categories = empty($line[1]) ? '' : explode(",", $line[1]);
            /*
                        * get Category IDs from titles
                        */                      
                        $cat_ids = array();
                        if ( !empty($categories) ) {
                          foreach ( $categories AS $category ) {
                            $cat_ids[] = get_cat_ID($category);
                              }
                            }

                        // assign column values to form fields
                        foreach ( $cols AS $col => $form_field_id ) {
                            $meta[$form_field_id] = $line[$col];
                        }

                        if ( $line && !empty($name) ) {
                            $this->addDataset($project_id, $name, $cat_ids, $meta);
                            $i++;
                        }
                  }
                  $l++;
                }
                fclose($handle);

                $this->setMessage(sprintf(__( '%d Datasets successfully imported', 'projectmanager' ), $i));
            } else {
                $this->setMessage( __('The file is not readable', 'projectmanager'), true );
            }
        } else {
            $this->setMessage(sprintf( __('The uploaded file could not be moved to %s.' ), parent::getFilePath()) );
        }
        @unlink($new_file); // remove file from server after import is done
    } else {
        $this->setMessage( __('The uploaded file seems to be empty', 'projectmanager'), true );
    }
}
Était-ce utile?

La solution

The explode function in php can misjudge double-byte characters, as can many other php functions unless the server is properly configured.

This article is a really good starting point for dealing with international character sets in PHP: http://blog.mayflower.de/archives/342-Multilingual-Websites-with-PHP.html

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top