I'm using the following code to create an xls file from php.
http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8
However, for some reason if the row "comments" is more than 255 characters, it doesn't output anything..
The code which writes the String to the XLS file is:
function xlsWriteString( $Row, $Col, $Value ) {
$L = strlen( $Value );
echo pack( "ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L );
echo $Value;
return;
};
Could someone help me get this to display the field regardless of how many characters are in the string?
Thanks
EDIT: I found this: http://support.microsoft.com/kb/213841 but not sure how to implement the work around into php..
ANOTHER EDIT: Even if anyone knows how to merge cells? That would also work?? :)
Sounds like it may be related to this issue.
Related
This question already has answers here:
Format bytes to kilobytes, megabytes, gigabytes
(28 answers)
Closed 3 years ago.
After thinking mine was in error,
I found LOT AT LOTS of scripts the do this:
https://gist.github.com/liunian/9338301
And there are several here at S.O. I used, but had the same annoying "B" as a size.
This issue seemed to rear it's ugly head when I switched to php v7.xxx
First issues is I have to typcase a floated number (or double) or else I get a
"A non well formed numeric value encountered"
After some research, apparently this is NOT a bug. At least that is how I read it.
So after typcasting it, the error goes away but the value returned is always a "B'
filesize = 87.5B (when it should be MB or GB).
I am pretty sure Javascript will work, but would rather keep it with php.
Thanks for looking
current live script that is producing a "B" only
public function readableBytes($size, $type='pc') { //ignore the pc - it is for something else - disabled for debugging
$size = (double)$size;
static $units = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$step = 1024;
$i = 0;
while (($size / $step) > 0.9) {
$size = $size / $step;
$i++;
}
return round($size, 2).$units[$i];
}// function readbbleBytes
This is a unique issue to those that use a loop for assigning (custom)template tags with an array field.
The filesize is one of many fields in an array I use.
I used a loop to go through each field and assign it to a template tag.
Not sure why the "B" came up. My suspicion is that the result of a "string" = 0 bytes. Even though It showed the actual size.
edit: spelling & claification
So to fix, in the middle of the loop, I forced the $array['filesize'] = readableBytes($array['filesize'])).
Before it was loop tag = fieldname.
foreach ($arr as $field=>$data) {
$arr['filesize'] = readableBytes($array['filesize'])); // fix was put here
$page = str_ireplace("{$field}", $data, $page);
}
The following seems to work (checked in phptester):
function human_filesize($bytes, $decimals = 2)
{
if ($bytes < 1024) {
return $bytes . ' B';
}
$factor = floor(log($bytes, 1024));
return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . ['B', 'KB', 'MB', 'GB', 'TB', 'PB'][$factor];
}
This is a cut and paste of a post by gladx in the following thread, where you'll find several optimised examples: https://gist.github.com/liunian/9338301#gistcomment-2183132
I'm looking for some help when replacing text from when i'm importing an XML file. I want to text-replace some values when importing, so it matches my categories, filter values etc. on my website.
I'm using this function. i wrote it myself with copy-pasting from internet (i'm not a coder) but now i need some help/advice.
<?php
// Text replace test function
function my_text_replace($x) {
for ($y = 0; $y < 2; $y = $y+1) {
$phrase = $x;
$old = array("Draaideurkast", "fout1 MRC", "Draaideurkast MRC", "Draaideurkast MRC");
$new = array("fout1", "fout2", "goed", "fout3");
$x = str_ireplace($old, $new, $phrase);
$y = $y+1;
return $x;
}
}
?>
Code Fix:
What happens is that i do not want a partial match replace, but only the complete value of $x. in the example the output should be 'goed'. it only should replace once when found. (but that is fixed with the for loop i think). the output should be case insensitive.
Advice question:
is this a correct way of replace (large amounts) of texts during an import? you guys know other best practises or plugins (wordpress) or tools..
Thanks for any response!
Harm
I am programming a PHP intranet and have problems with extracting of data from older system. It was created with Sharepoint and MSSQL. It stores the data terribly unclean.
I found the desired data searching by keyword only by accident with help of HeidiSQL (db management tool). Its fulltext filter is searching also in varbinary columns and one time it converted the varbinary content to meaningful xml data (I saw header + 50 next letters), but only for a second, then it disappeared back to the original value.
I moved data from SQLserver to MySQL via CSV export, so I can work with PHP.
The varbinary contains this value:
0xEFBBBF2F2A205F6C6369643D223130353122205F76657273696F6E3D2231342E302E34373632220D0A5F4C6F63616C42696E64696E67202A2F0D7461626C652E6D732D646973632D6261720D7B0D206261636B67726F756E642D636F6C6F723A233539353935393B0D206261636B67726F756E642D696D6167653A75726C28717569636B6C61756E63686865616465722D44434239423136342E6769662E706E673F63746167293B0D6261636B67726F756E642D7265706561743A7265706561742D783B0D20626F726465723A31707820736F6C696420233236323632363B0D746578742D616C69676E3A6C6566743B0D7D0D7461626C652E6D732D646973632048520D7B0D6865696768743A3170783B0D20636F6C6F723A233539353935393B0D7D0D7461626C652E6D732D646973632074647B0D666F6E742D73697A653A3870743B0D666F6E742D66616D696C793A7461686F6D612C73616E732D73657269663B0D7D0D7461626C652E6D732D646973632074640D7B0D766572746963616C2D616C69676E3A746F703B0D7D0D2E6D732D6469736320617B0D20636F6C6F723A234138323230433B0D746578742D6465636F726174696F6E3A6E6F6E653B0D7D0D2E6D732D6469736320613A686F7665727B0D20636F6C6F723A233030303B0D746578742D6465636F726174696F6E3A756E6465726C696E653B0D7D0D2E6D732D6469736320613A766973697465647B0D20636F6C6F723A234445343631433B0D746578742D6465636F726174696F6E3A6E6F6E653B0D7D0D2E6D732D6469736320613A766973697465643A686F7665727B0D20636F6C6F723A233030303B0D746578742D6465636F726174696F6E3A756E6465726C696E653B0D7D0D7461626C652E6D732D646973632074640D7B0D70616464696E673A303B0D20636F6C6F723A233346334633463B0D7D0D2E6D732D646973632D6E6F7061642C7461626C652074722074642E6D732D646973632D6E6F7061642C7461626C652E6D732D646973632D6E6F7061642074640D7B0D70616464696E673A3070783B0D7D0D7461626C652074722074642E6D732D646973632D70616461626F76657B0D70616464696E673A3570782035707820357078203570783B0D7D0D7461626C652E6D732D646973632D6261722074640D7B0D766572746963616C2D616C69676E3A6D6964646C653B0D6865696768743A323270783B0D70616464696E672D6C6566743A3570783B0D7D0D7461626C652E6D732D646973632D62617220494D470D7B0D766572746963616C2D616C69676E3A6D6964646C653B0D7D0D7461626C652E6D732D646973632D62617220420D7B0D766572746963616C2D616C69676E3A3130253B0D70616464696E672D72696768743A3570783B0D7D0D74642E6D732D646973632D626F7264657265642C74642E6D732D646973632D626F7264657265642D6E6F6C6566740D7B0D20626F726465723A30707820736F6C696420233539353935393B0D626F726465722D746F702D77696474683A3070783B0D7D0D7461626C652074722074642E6D732D646973632D626F7264657265642D6E6F6C6566740D7B0D626F726465722D6C6566742D77696474683A3170783B0D70616464696E673A30707820313070782035707820313070783B0D666F6E742D66616D696C793A76657264616E613B0D666F6E742D73697A653A31656D3B0D746578742D616C69676E3A6C6566743B0D7D0D7461626C652074722074642E6D732D646973632D626F7264657265647B0D70616464696E673A3070782030707820357078203570783B0D7D0D6469762E6D732D646973632D726F6F742D626F6479206469763A66697273742D6368696C6420703A66697273742D6368696C647B0D6D617267696E2D746F703A3070783B0D7D0D
How can I convert it back to meaningful XML? I tried these PHP functions: hexdec(), base64_encode(), base64_decode() - but none is working.
There is 10.000 rows there... Please help me :) Thank you in advance.
This works well also in PHP <5.4, thank you for your time:
if ( !function_exists( 'hex2bin' ) ) {
function hex2bin( $str ) {
$sbin = "";
$len = strlen( $str );
for ( $i = 0; $i < $len; $i += 2 ) {
$sbin .= pack( "H*", substr( $str, $i, 2 ) );
}
return $sbin;
}
}
I have been asked/told to convert a foxpro function to PHP, however I know nothing about foxpro.
PARAMETERS cCkey
LOCAL cKey
cKey = SUBSTR(SYS(2015),2)+PADL(LTRIM(STR(INT(IIF(INT(RAND()*1000000000) = 851390329,RAND(-1),RAND())*1000000),6)),6,"0")
RETURN cKey
Above is the function they are wanting to use in a system that is being built in php to integrate with the foxpro databases.
Some of the functions are familiar from PHP, but others like the "SYS", and "IIF" are not, and being that I know someone on here will be able to take one look at it, and know exactly what it is doing.
Mind helping me out? Thanks in advance.
Sys(2015) is a handy VFP function which returns a value unique to that session of VFP. You can read it here
Iif is inline if-else-endif statement .. like Excel does
Updated
SYS(2015) in PHP ? I don't know .. but if we talking about random string in PHP, you can use this
function rand_string( $length ) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$size = strlen( $chars );
for( $i = 0; $i < $length; $i++ ) {
$str .= $chars[ rand( 0, $size - 1 ) ];
}
return $str;
}
I got that from this link and got the basic idea from this link
About the other part MAYBE like this :
$randomresult = 0
$srandom = ""
If (INT(RAND()*1000000000) = 851390329)
{
$randomresult = int(rand(-1)) * 1000000
}
else
{
$randomresult = int(rand()) * 1000000
}
$srandom=str_pad(ltrim(strval($randomresult),"0")),6,"0",STR_PAD_LEFT)
So, MAYBE we can make the foxpro code like this in PHP :
$cKey = rand_string(10) . str_pad(ltrim(strval($randomresult),"0")),6,"0",STR_PAD_LEFT)
At least you can the idea .....
SYS(2015) Returns a unique 10-character procedure name that begins with an underscore followed by a combination of letters and numbers.
http://msdn.microsoft.com/en-us/library/684by7c1(v=vs.80).aspx
IIF Returns one of two values depending on the value of a logical expression.
http://msdn.microsoft.com/en-us/library/7ttt15k6(v=vs.80).aspx
With this information I think you can at least take a stab at creating a PHP function and then showing some PHP and asking for help if needed.
I have a PHP application that downloads MySQL table data to a spreadsheet on the client machine. I found some code in numerous places on the web that works fine for Open Office Scalc on a machine running Redhat. The problem I ran into was when I tried to download to an MS Excel on a Windows PC rather than to an Open Office spreadsheet. The problem seems to be associated with the length of the string inserted into a cell. If it is too long, Excel thinks the file is corrupt and won't load it. "Too long" seems to be about 255 characters, even though the MS Excel specs say 32,000 is the maximum length.
To investigate the problem further, I tried downloading the same data as a tab-separated values file and let Excel convert it to a spreadsheet. Using that method, there was no problem loading very long strings into the spreadsheet cells. So strings much longer than 255 characters can in fact be inserted into Excel cells, but just not with the code I am using, even though that code works with Open Office. Using tsv files would not solve our problem, because the long strings have carriage returns that we want to retain, and carriage returns are interpreted as row separators when a tsv file is loaded into a spreadsheet.
The PHP function that writes a string to a spreadsheet cell is:
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
The other necessary code for transfers to spreadsheets in addition to the above function can be found at:
http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8
I haven't found any explanations as to the meanings of the various arguments passed to the "pack" function in the above code, and I'm wondering if changing one the arguments in the function above could solve the problem.
So, if anyone has a solution to this problem, I'd be interested in hearing it.
...unless your text has double quotes.
What you should use is proper delimiters from the ASCII char set.
Seq Dec Hex Acro Name
^\ 28 1C FS File Separator
^] 29 1D GS Group separator
^^ 30 1E RS Record Separator
^_ 31 1F US Unit separator
I don't know php but in perl a print statement might look like:
print chr(30), "Field1", chr(30), "Field2", chr(30), "Field3", chr(31);
I have written a simple approach to generate absolute excel file.
A simple function.
First you convert your contents to an Array (it is very simple and everybody know.). Then pass the array to my function given below.
<?php
//array to excel
function arrayToExcel($array, $filename='List')
{
if (!is_array($array) or !is_array($array[0])) return false;
$xl = pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); //begining of excel file
$i = 0;
foreach ($array as $key => $val)
{
$j=0;
foreach ($val as $cell_key => $cell_val)
{
$cell_val = trim($cell_val);
$length = strlen($cell_val);
//checking cell value is a number and the length not equal to zero
if(preg_match('/^[0-9]*(\.[0-9]+)?$/', $cell_val) && $length!=0) {
$xl .= pack("sssss", 0x203, 14, $i, $j, 0x0); //writing number column
$xl .= pack("d", $cell_val);
}
else {
$xl .= pack("ssssss", 0x204, 8 + $length, $i, $j, 0x0, $length); //writing string column
$xl .= $cell_val;
}
$j++;
} //end of 2nd foreach
$i++;
} //end of first foreach
$xl .= pack("ss", 0x0A, 0x00); //end of excel file
$filename = $filename.".xls";
header("Pragma: public");
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=$filename");
header("Cache-Control: no-cache");
echo $xl;
} //end of arrayToExcel function
//eg: arrayToExcel($myarray, "contact_list");
?>
Surround your cell values with double quotes "", it will allow you to import the data in a tsv or csv format without having to worry about the carriage returns being interpreted as a new row. You could also use the fputcsv function from PHP to export a tsv or csv file that excel can read.
This is the slightly more efficient version of their functions thank you linked too, but I don't know if the format they output is actually correct for excel or not.
function xlsBOF(){
echo pack("s6", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
}
function xlsEOF(){
echo pack("ss", 0x0A, 0x00);
}
function xlsWriteNumber($Row, $Col, $Value){
echo pack("s5d", 0x203, 14, $Row, $Col, 0x0, $Value);
}
function xlsWriteLabel($Row, $Col, $Value){
$L = strlen($Value);
echo pack("s6A*", 0x204, 8 + $L, $Row, $Col, 0x0, $L, $Value);
}