Jump to content

Few Special Characters not getting displayed


Go to solution Solved by cloetensbrecht,

Recommended Posts

Hi,

 

I got a string with special characters and in the table when i see the text, all the texts are inserted properly with the special characters. But when I use the below code to generate CSV file, for few of the special characters like - , ' and ... it displays â€“, â€™ and â€¦. So could someone guide me what changed I have to do in order to get the characters get displayed properly in my CSV file ?


<?php  
  include("dbconnect.php");

  if(isset($_POST['groupname'], $_POST['decks'], $_POST['rows'])){

    $groupname = $_POST['groupname'];
    $decks = $_POST['decks'];
    $rows = $_POST['rows'];    
    $pickdeckrows = $rows/$decks;

    $zip = new ZipArchive();
    $zipname = $groupname.'.zip';
    $zip -> open($zipname, ZipArchive::CREATE);
    $j=1;
    for($i=1; $i<=$decks; $i++){

        $stmt = $dbconnect->prepare("SELECT v.deckid, LPAD(v.decknumber,3,0) as decknumber, v.text  FROM texttable v WHERE  v.groupname=:groupname ORDER BY v.id LIMIT :pickdeckrows");
        $stmt->bindValue(':groupname', $groupname);
        $stmt->bindValue(':pickdeckrows', $pickdeckrows);
        $stmt->execute();

        $output = fopen('php://temp/maxmemory:1048576', 'w');
        if (false === $output) {
            die('Failed to create temporary file');
        }

        while($row = $stmt -> fetch()){
                
            $length += fputcsv($output, [$groupname.$i.$row["decknumber"], $row["text"]
            ]);
        }

        rewind($output);     
        $zip->addFromString($groupname.'Deck '.$j.'.csv', stream_get_contents($output) );
        fpassthru($output);
        fclose($output);
        $j++;  
      }          
    
      $zip->close(); 

      /* To download the zip file from browser use below commented code */
      header('Content-Type: application/zip');
      header('Content-disposition: attachment; filename='.$zipname);
      header('Content-Length: '. filesize($zipname));
      header('Expires: 0');
      header('Cache-Control: private');
      header('Pragma: private');
      ob_clean();
      flush();
      readfile($zipname);
      unlink($zipname);  
  }
  
  else {
    echo "Not set!!!";
  }
?>

Thanks.

Have you added the following mysql query after your db connection?

SET NAMES utf8

 

Right now I am using the below db connection. PHP version is 5.6

$host = '';
$db   = '';
$user = '';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

$dbconnect = new PDO($dsn, $user, $pass, $opt);

So, i guess it should work if i change to utf8, but i did change and check the below string:

 

~ @ ! # $ % ^ & * ( ) _ + } { " | \ /.,\ ?>|":

 

but in my CSV file it shows :

 

~ @ ! # $ % ^ & * ( ) _ + } { " | \ /.,\ ?>|":

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.