export mysql result to excel - php

I found a little script that will export information to an xls file, but I can't seem to get it working. The original code is found here: http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8
And here's my code:
// Query Database
$query = 'SELECT * FROM #__db_clients WHERE published = '1' AND companyid IN (1,2) AND country = 'Africa' ORDER BY state ASC';
$db->setQuery($query);
$rows = $db->loadObjectList();
if ($rows) {
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
};
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
};
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
};
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
};
// Send Header
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=export.xls ");
header("Content-Transfer-Encoding: binary ");
// XLS Data Cell
xlsBOF();
xlsWriteLabel(1,0,"Company ID");
xlsWriteLabel(1,1,"Company Name");
xlsWriteLabel(1,2,"Address");
xlsWriteLabel(1,3,"Address 2");
xlsWriteLabel(1,4,"Suburb");
xlsWriteLabel(1,5,"City");
xlsWriteLabel(1,6,"State");
xlsWriteLabel(1,7,"Post Code");
xlsWriteLabel(1,8,"Country");
xlsWriteLabel(1,9,"Date");
xlsWriteLabel(1,10,"Phone Business");
xlsWriteLabel(1,11,"Phone Direct");
xlsWriteLabel(1,12,"Fax");
xlsWriteLabel(1,13,"Phone Mobile");
xlsWriteLabel(1,14,"Phone Personal");
xlsWriteLabel(1,15,"Discount");
xlsWriteLabel(1,16,"Title");
xlsWriteLabel(1,17,"Name");
xlsWriteLabel(1,18,"Surname");
xlsWriteLabel(1,19,"Position");
xlsWriteLabel(1,20,"Email");
xlsWriteLabel(1,21,"Contact");
xlsWriteLabel(1,22,"Introduced");
xlsWriteLabel(1,23,"Comments");
xlsWriteLabel(1,24,"Type");
xlsWriteLabel(1,25,"Status");
xlsWriteLabel(1,26,"Rating");
xlsWriteLabel(1,27,"Credit Terms");
xlsWriteLabel(1,28,"Classifications");
$xlsRow = 2;
$i = 0;
foreach ($rows as $item) {
$companyid = $rows[$i]->companyid;
$company = $rows[$i]->company;
$address = $rows[$i]->address;
$address2 = $rows[$i]->address2;
$suburb = $rows[$i]->suburb;
$city = $rows[$i]->city;
$state = $rows[$i]->state;
$pcode = $rows[$i]->pcode;
$country = $rows[$i]->country;
$date = $rows[$i]->date;
$phone_b = $rows[$i]->phone_b;
$phone_d = $rows[$i]->phone_d;
$phone_f = $rows[$i]->phone_f;
$phone_m = $rows[$i]->phone_m;
$phone_p = $rows[$i]->phone_p;
$discount = $rows[$i]->discount;
$title = $rows[$i]->title;
$name = $rows[$i]->name;
$surname = $rows[$i]->surname;
$position = $rows[$i]->position;
$email = $rows[$i]->email;
$contact = $rows[$i]->contact;
$introduced = $rows[$i]->introduced;
$comments = $rows[$i]->comments;
$type = $rows[$i]->type;
$status = $rows[$i]->status;
$rating = $rows[$i]->rating;
$cterms = $rows[$i]->cterms;
$classifactions = $rows[$i]->classifactions;
xlsWriteNumber($xlsRow,0,"$companyid");
xlsWriteNumber($xlsRow,1,"$company");
xlsWriteNumber($xlsRow,2,"$address");
xlsWriteNumber($xlsRow,3,"$address2");
xlsWriteNumber($xlsRow,4,"$suburb");
xlsWriteNumber($xlsRow,5,"$city");
xlsWriteNumber($xlsRow,6,"$state");
xlsWriteNumber($xlsRow,7,"$pcode");
xlsWriteNumber($xlsRow,8,"$country");
xlsWriteNumber($xlsRow,9,"$date");
xlsWriteNumber($xlsRow,10,"$phone_b");
xlsWriteNumber($xlsRow,11,"$phone_d");
xlsWriteNumber($xlsRow,12,"$phone_f");
xlsWriteNumber($xlsRow,13,"$phone_m");
xlsWriteNumber($xlsRow,14,"$phone_p");
xlsWriteNumber($xlsRow,15,"$discount");
xlsWriteNumber($xlsRow,16,"$title");
xlsWriteNumber($xlsRow,17,"$name");
xlsWriteNumber($xlsRow,18,"$surname");
xlsWriteNumber($xlsRow,19,"$position");
xlsWriteNumber($xlsRow,20,"$email");
xlsWriteNumber($xlsRow,21,"$contact");
xlsWriteNumber($xlsRow,22,"$introduced");
xlsWriteNumber($xlsRow,23,"$comments");
xlsWriteNumber($xlsRow,24,"$type");
xlsWriteNumber($xlsRow,25,"$rating");
xlsWriteNumber($xlsRow,26,"$status");
xlsWriteNumber($xlsRow,27,"$cterms");
xlsWriteNumber($xlsRow,28,"$classifactions");
$xlsRow++;
$i++;
};
xlsEOF();
exit();
};
I echoed out each row to make sure data is being passed through, which is ok, but for some reason, the xls file is spitting out like this:
Company ID Company Name Address
1 0 2
2 0 0
And so on, it seems to be somehow passing through a number, but not the actual information.. Can anyone help me out? Or point me to a decent export to excel (xls).. :)
EDIT:
If anyone can figure out how to set xlsWriteLabel to be bold, that would be fantastic :)

If the actual information is strings, this won't work. Pack("d" is trying to cast it as a Double instead of a String.
You need to create a xlsWriteString function.
Looks like there's one here: http://hunter.forumotion.com/forum-f9/topic-t98.htm
function xlsWriteString( $Row , $Col , $Value )
{
$L = strlen( $Value );
echo pack( "ssssss" , 0x204 , 8 + $L , $Row , $Col , 0x0 , $L );
echo $Value;
return;
}

I think if you client are using Office 2007, you have better to look into the Open Document.
there is an implementation in php there. Also the Excel 2007 viewer is free so.

Related

Function to export a data report to excel in php with dynamic no of fields and rows

I am trying to develop a function for exporting the report contents to a xls file and giving users an option to have an xls file downloaded for the results they are seeing on webpage. Below is the code i have tried:
<?php
class export
{
public function exportxls($cols,$values)
{
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
}
function xlsWriteLabel($Row, $Col, $Value) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
}
// prepare headers information
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=\"export_".date("Y-m-d").".xls\"");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
// start exporting
xlsBOF();
$j=1;
$ret = array_map (
function ($_) {return explode (',', $_);},
explode (';', $values)
);
$colarray=explode(',',$cols);
$number=count($colarray);
for($i=0;$i<$number;$i++)
{
xlsWriteLabel(0,$i,$colarray[$i]);
}
foreach($ret as $key->$value)
{
xlsWriteLabel($j,$key,$value[$key]);
$j=$j+1;
}
xlsEOF();
}
}
?>
and here is the index file from which function is called:
<?php
include 'newxls.php';
$obj=new export();
$obj->exportxls("id,name,class","1,var,btech;2,man,mtech");
?>
Please do help me; I am not getting the required output in excel file.
Thanks in advance
Your code looking good except few changes needed
Change this code
foreach($ret as $key->$value)
{
xlsWriteLabel($j,$key,$value[$key]);
$j=$j+1;
}
Replace with this
foreach ($ret as $key => $value) {
foreach($value as $k=>$v){
xlsWriteLabel($j, $k, $v);
}
$j = $j + 1;
}
All remaining code looking fine.
Try This,
$filename ="excelreport";
function exportexcel($fields = array(), $values = array()){
foreach($fields as $field){
$contents .= $field." \t";
}
$contents. = " \n";
foreach($values as $value){
$contents.= $value[0]. " \t";
$contents.= $value[1]. " \t";
$contents.= $value[2]. " \n";
}
return $contents;
}
header('Content-type: application/ms-excel');
header("Content-Disposition: attachment; filename=". $filename . date("Y-m-d-H-i") .".xls");
header("Cache-Control: no-cache, must-revalidate");
$contents = exportexcel(array("id,name,class"),array(array('1','test','btech'),array('2','man','mtech')));
echo $contents;

PHP export excel working in Chrome and IE but in Firefox (Get file with no extension)

My code works in Chrome and IE perfectly but when I try to download the file using Firefox it has no extension. I already tried different solutions given in stackoverflow with the same problem of mine but nothing works. Is there anybody who can help me with this?
this is my PHP code:
function empsal_to_excel(){
$empsal = $_SESSION['empsalParam'];
$rows = $this->SalaryReport->getBranchSalary($empsal['branch']);
$numrows = count($rows);
// Send Header
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=Employee Salary Report:".$_SESSION['comp_name'].".xls "); // à¹à¸¥à¹‰à¸§à¸™à¸µà¹ˆà¸à¹‡à¸Šà¸·à¹ˆà¸­à¹„ฟล์
header("Content-Transfer-Encoding: binary ");
// XLS Data Cell
$this->xlsBOF();
$this->xlsWriteLabel(1,3,$_SESSION['comp_name']);
$this->xlsWriteLabel(4,0,"REPORT TITLE : ");
$this->xlsWriteLabel(4,1,"Employee Salary Report");
$this->xlsWriteLabel(4,6,"REPORT PRINT DATE : ");
$this->xlsWriteLabel(4,7, date('Y-m-d'));
$this->xlsWriteLabel(7,0,"EMPLOYEE NO.");
$this->xlsWriteLabel(7,1,"EMPLOYEE NAME");
$this->xlsWriteLabel(7,2,"JOB DESCRIPTION");
$this->xlsWriteLabel(7,3,"DEPARTMENT");
$this->xlsWriteLabel(7,4,"BRANCH");
$this->xlsWriteLabel(7,5,"SHIFTING SCHEDULE");
$this->xlsWriteLabel(7,6,"SALARY FREQUENCY");
$this->xlsWriteLabel(7,7,"SALARY");
$xlsRow = 9;
$t=0;
$to_log=array();
while($t<$numrows){
$y=0;
foreach($rows[$t] as $key=>$items){
$to_log[]=$key."=>".$items;
$this->xlsWriteLabel($xlsRow,$y,$items);
$y++;
}
$xlsRow++;
$t++;
}
$this->xlsEOF();
$this->_audittrailbase->_log('EXPORT_TO_EXCEL', 'Report' ,$to_log,null,$_SESSION['branch']);
exit();
}
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
//return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
// return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
//return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
//return;
}

Creating Excel file from MySQL

I am trying to generate an XLS file from a table in a MySQL DB, but the Excel file is not properly formatted & given error when the Excel file generated "The file which you are trying to open is in different format than one specified". When the file is opened the data is not properly formatted.
Any ideas what I am missing?
<?php
$host = 'XXXXXXX';
$dbname = 'XXXXXXXX';
$username = 'XXXXXXXX';
$password = 'XXXXXXXX';
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
echo "Connected to $dbname at $host successfully.";
$conn = null;
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
$q = "SELECT * FROM tablename";
$qr = mysql_query( $q ) or die( mysql_error() );
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=export_".$dbtable.".xls ");
header("Content-Transfer-Encoding: binary ");
xlsBOF();
$col = 0;
$row = 0;
$first = true;
while( $qrow = mysql_fetch_assoc( $qr ) )
{
if( $first )
{
foreach( $qrow as $k => $v )
{
xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) );
$col++;
}
$col = 0;
$row++;
$first = false;
}
// go through the data
foreach( $qrow as $k => $v )
{
// write it out
xlsWriteLabel( $row, $col, $v );
$col++;
}
// reset col and goto next row
$col = 0;
$row++;
}
xlsEOF();
exit();
I'm not sure about .xls but for outputting a MySQL result as a CSV table the fputcsv function does it without much fuss:
// Clear any previous output
ob_end_clean();
// I assume you already have your $result
$num_fields = mysql_num_fields($result);
// Fetch MySQL result headers
$headers = array();
$headers[] = "[Row]";
for ($i = 0; $i < $num_fields; $i++) {
$headers[] = strtoupper(mysql_field_name($result , $i));
}
// Filename with current date
$current_date = date("y/m/d");
$filename = "MyFileName" . $current_date . ".csv";
// Open php output stream and write headers
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.$filename);
header('Pragma: no-cache');
header('Expires: 0');
echo "Title of Your CSV File\n\n";
// Write mysql headers to csv
fputcsv($fp, $headers);
$row_tally = 0;
// Write mysql rows to csv
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$row_tally = $row_tally + 1;
echo $row_tally.",";
fputcsv($fp, array_values($row));
}
die;
}
use http://phpexcel.codeplex.com/
C'est la meilleur solution pour générer un fichier excel
Vous pouvez même créer plusieurs feuilles dans le fichier et formater les cellules (couleur, police, bordure, ...)
Google translate:
This is the best solution to generate an excel file You can even create multiple sheets in the file and format the cells (color, font, border, ...)
<?php
//download.php page code
//THIS PROGRAM WILL FETCH THE RESULT OF SQL QUERY AND WILL DOWNLOAD IT. (IF YOU HAVE ANY QUERY CONTACT:rahulpatel541#gmail.com)
//include the database file connection
include_once('database.php');
//will work if the link is set in the indx.php page
if(isset($_GET['name']))
{
$name=$_GET['name']; //to rename the file
header('Content-Disposition: attachment; filename='.$name.'.xls');
header('Cache-Control: no-cache, no-store, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header('Content-Type: application/x-msexcel; charset=windows-1251; format=attachment;');
$msg="";
$var="";
//write your query
$sql="select * from tablename";
$res = mysql_query($sql);
$numcolumn = mysql_num_fields($res); //will fetch number of field in table
$msg="<table><tr><td>Sl No</td>";
for ( $i = 0; $i < $numcolumn; $i++ ) {
$msg.="<td>";
$msg.= mysql_field_name($res, $i); //will store column name of the table to msg variable
$msg.="</td>";
}
$msg.="</tr>";
$i=0;
$count=1; //used to print sl.no
while($row=mysql_fetch_array($res)) //fetch all the row as array
{
$msg.="<tr><td>".$count."</td>";
for($i=0;$i< $numcolumn;$i++)
{
$var=$row[$i]; //will store all the values of row
$msg.="<td>".$var."</td>";
}
$count=$count+1;
$msg.="</tr>";
}
$msg.="</table>";
echo $msg; //will print the content in the exel page
}
?>
<?php
//index.php page
$name="any file name";
echo "<a href='download.php?name=".$name."'>Click to download</a>"; //link to download file
?>
Here is simple Excel file generation function, very fast and exactly .xls file.
$filename = "sample_php_excel.xls";
$data = array(
array("User Name" => "Abid Ali", "Q1" => "$32055", "Q2" => "$31067", "Q3" => 32045, "Q4" => 39043),
array("User Name" => "Sajid Ali", "Q1" => "$25080", "Q2" => "$20677", "Q3" => 32025, "Q4" => 34010),
array("User Name" => "Wajid Ali", "Q1" => "$93067", "Q2" => "$98075", "Q3" => 95404, "Q4" => 102055),
);
to_xls($data, $filename);
function to_xls($data, $filename){
$fp = fopen($filename, "w+");
$str = pack(str_repeat("s", 6), 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); // s | v
fwrite($fp, $str);
if (is_array($data) && !empty($data)){
$row = 0;
foreach (array_values($data) as $_data){
if (is_array($_data) && !empty($_data)){
if ($row == 0){
foreach (array_keys($_data) as $col => $val){
_xlsWriteCell($row, $col, $val, $fp);
}
$row++;
}
foreach (array_values($_data) as $col => $val){
_xlsWriteCell($row, $col, $val, $fp);
}
$row++;
}
}
}
$str = pack(str_repeat("s", 2), 0x0A, 0x00);
fwrite($fp, $str);
fclose($fp);
}
function _xlsWriteCell($row, $col, $val, $fp){
if (is_float($val) || is_int($val)){
$str = pack(str_repeat("s", 5), 0x203, 14, $row, $col, 0x0);
$str .= pack("d", $val);
} else {
$l = strlen($val);
$str = pack(str_repeat("s", 6), 0x204, 8 + $l, $row, $col, 0x0, $l);
$str .= $val;
}
fwrite($fp, $str);
}
<?php
ob_end_clean();
$num_fields = mysql_num_fields($result);
$headers = array();
$headers[] = "[Row]";
for ($i = 0; $i < $num_fields; $i++)
$headers[] = strtoupper(mysql_field_name($result , $i));
$current_date = date("y/m/d");
$filename = "MyFileName" . $current_date . ".csv";
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='.$filename);
header('Pragma: no-cache');
header('Expires: 0');
echo "Title of Your CSV File\n\n";
fputcsv($fp, $headers);
$row_tally = 0;
// Write mysql rows to csv
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$row_tally = $row_tally + 1;
echo $row_tally.",";
fputcsv($fp, array_values($row));
}
die;
}
?>

PHP Mysql table conversion to excel:The file you are trying to open is in a different format than specified by the extension

I have a code that converts mysql table to excel sheet. It converts and the excel sheet is downloaded but whenever I try to open the file it gives an error msg in excel 2007:"The file you are trying to open is in a different format than specified by the extension...".The excel sheet opens after I click on 'Yes' but it is nothing but a white blank sheet.I have seen the similar posts but none of them solves my problem...Please help...Thank you
<?php
include('dbcon.php');
?>
<?php
$sql = "SELECT * FROM stu_gen_info";
$rec = mysql_query($sql) or die (mysql_error());
$num_fields = mysql_num_fields($rec);
for($i = 0; $i < $num_fields; $i++ )
{
$header .= mysql_field_name($rec,$i)."\\t";
}
while($row = mysql_fetch_row($rec))
{
$line = '';
foreach($row as $value)
{
if((!isset($value)) || ($value == ""))
{
$value = "\\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\\n";
}
$data = str_replace("\\r" , "" , $data);
if ($data == "")
{
$data = "\\n No Record Found!\n";
}
header("Content-Type: application/vnd.ms-excel");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=reports.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\\n$data";
?>
try adding this
header("Content-Type: application/vnd.ms-excel");

MySQL to .XLS Encoding issue

I have got problem with encoding while I transferring data from MySQL to .XLS file. Table is in "utf8_czech_ci" and PHP script in UTF-8. I always get characters like "ěščřžýáíé" in bad form like "ěšÄřžýáíé". There is my script
<?php
mb_http_input("utf-8");
mb_http_output("utf-8");
$dbhost = "XXXXXXXXXXX";
$dbuser = "XXXXXXXXXXX";
$dbpass = "XXXXXXXXXXX";
$dbname = "XXXXXXXXXXX";
$dbtable = $_GET['table'];
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
$dbc = mysql_connect( $dbhost , $dbuser , $dbpass ) or die( mysql_error() );
mysql_query("set names utf8;");
mysql_select_db( $dbname );
$q = "SELECT * FROM ".$dbtable."";
$qr = mysql_query( $q ) or die( mysql_error() );
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment;filename=export_".$dbtable.".xls ");
header("Content-Transfer-Encoding: binary ");
xlsBOF();
$col = 0;
$row = 0;
$first = true;
while( $qrow = mysql_fetch_assoc( $qr ) )
{
if( $first )
{
foreach( $qrow as $k => $v )
{
xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) );
$col++;
}
$col = 0;
$row++;
$first = false;
}
foreach( $qrow as $k => $v )
{
xlsWriteLabel( $row, $col, $v );
$col++;
}
$col = 0;
$row++;
}
xlsEOF();
exit();
Thanks for responses ...
An Excel file uses a codepage block to identify the character set being used within the file, but by default it will use the locale codepage. If you want to force UTF-8, then you'll need to write a UTF-8 codepage block as well
$record = 0x0042; // Record identifier
$length = 0x0002; // Number of bytes to follow
$cv = 0x04B0; // The UTF-8 code page
$header = pack('vv', $record, $length);
$data = pack('v', $cv);

Categories