i am using (shuchkin/simplexlsxgen) to generate xlsx file which works fine.
My data contain decimal numbers such as (0.19) which are written in the SQL DB, but instead writting them with dott, the (simplexlsxgen) convert them to (0,19) comma decimal numbers.
is there a way to prevent changing the decimal Dott to comma, before generating?
Thanks in advance.
//convert test_bulk.csv to Xlsx
function csvToArray($csvFile)
{
$file_to_read = fopen($csvFile, 'r');
while (!feof($file_to_read))
{
$lines[] = fgetcsv($file_to_read, 1000, ';');
}
fclose($file_to_read);
return $lines;
}
//read the csv file into an array
$csvFile = 'test_bulk.csv';
$csv = csvToArray($csvFile);
Shuchkin\SimpleXLSXGen::fromArray($csv)->saveAs('final_bulk.xlsx');
by adding "\0". before the targeted string, it will turn it to be a RAW STRING. which will make this super Tool write it without changing the decimal dott to comma.
you all gotta try this amazing fast effective tool by the PHP Excel Old school Master (Sergey Shuchkin).
https://github.com/shuchkin
Related
I know that this question might have been answered before. but I still haven't been able to solve it.
my data comes from a CSV file where the data looks like this 1,32 BUT i also have some text in the variable $getdata half of it is VARCHAR and the other half is DECIMAL.
but when I look at my site the data is like this 1.00
So my question is How can I change the decimal separator.
The data is located in the $getdata variable in the bottom part in the code piece
if (isset($_POST['btn-upload'])){
copy("$sourcepath/$latest_filename","$copy/$latest_filename");
// Here i split the csv file, from second line. and using the first and fourth lines as the headers.
if (($openfile = fopen("$copy/$latest_filename", "r")) !== false) {
$header1 = fgetcsv($openfile, 1000, ";"); // consume, but don't use
$csv->createPalleTable($latest_filename);
$csv->insertPalleTable($latest_filename, array_map("utf8_encode", fgetcsv($openfile, 1000, ";")));
$delimiting_row = fgetcsv($openfile, 1000, ";"); // consume, but don't use
$header2 = fgetcsv($openfile, 1000, ";"); // consume, but don't use
$csv->createCsvTable($latest_filename);
while ($getdata = fgetcsv($openfile, 1000, ";")) {
$csv->insertCsvTable($latest_filename, array_map("utf8_encode", $getdata));
}
}
}
I haven't uploaded the full code since that would be way to much code. I'm pretty sure the problem is located at the bottom of the code snippet. if you need any information about the code I'll be happy to answer.
try using number format with a simple str_replace to replace the comma's so the float can be parsed.
number_format(floatval(str_replace(',', '.', str_replace('.', '', $your_number_here))), '.', '', 2)
If you want a separate readable function instead of a one-liner:
function convert_decimal($your_number_here, $decimal_places = 2)
{
$without_thousands_dots = str_replace('.', '', $your_number_here);
$replace_comma_for_dot = str_replace(',', '.', $without_thousands_dots);
$float_number = floatval($replace_comma_for_dot);
return number_format($float_number, '.', '', $decimal_places)
}
You could skip the floatval part if you don't wish to use rounding or use the number as an actual number and just keep it at returning the $replace_comma_for_dot variable.
For reference how to parse the decimal numbers to floats so you can use number format:
Converting a number with comma as decimal point to float
i wanted to rename files from databse. so..i wrote bellow.
it works fine except for names with long length int.
(ex: bartmp_9404865346.jpg does not work but bartmp_585558.jpg is working)
$subject = '[img]http://www.example.org/users/uploads/bartmp_9404865346.jpg[/img]
Hello world
[img]http://www.example.org/users/uploads/bartmp_585558.jpg[/img]';
preg_match_all('/\[img\](.*?)\[\/img\]/', $subject, $files);
foreach ($files[1] as $file) {
$n = sscanf($file, "http://www.example.org/users/uploads/bartmp_%d.jpg");
$refile = sprintf("http://www.example.org/users/uploads/mybar_%d.jpg", $n[0]);
rename($file, $refile);
}
can you give me any alter way to do this or a little hint to modify this.
thanks.
The %d format specifier only accepts numbers that would fit in an integer (depending on the platform that would be 2^31 or 2^63); without losing precision, in this case, a regular expression may work better:
if (preg_match('#^http://www.example.org/users/uploads/bartmp_(\d+)\.jpg$#', $file, $matches)) {
$refile = sprintf('http://www.example.org/users/uploads/mybar_%s.jpg', $matches[1]);
rename($file, $refile);
}
The above expression matches only digits but stores the match as a string value, so it won't lose numeric precision.
You are using %d for decimal which seems superficially correct:
$n = sscanf($file, "http://www.example.org/users/uploads/bartmp_%d.jpg");
$refile = sprintf("http://www.example.org/users/uploads/mybar_%d.jpg", $n[0]);
The issue is the max numerical value in PHP—and other languages compiled as 32-bit—is 2147483647 so 9404865346 won’t fly. Instead you should look to extract the value as a string instead like this:
$n = sscanf($file, "http://www.example.org/users/uploads/bartmp_%s.jpg");
$refile = sprintf("http://www.example.org/users/uploads/mybar_%s", $n[0]);
I'm trying to write a binary file from hex string.
For example, if my hex string is C27EF0EC, then the hex file should contain ASCII characters for C2, 7E, F0 and EC.
How do I do this in PHP?
Here's what I've tried:
$s="";
for ($i=0; $i<count($h); $i++) {
$s+=pack("C*", "0x".$h[$i]);
}
$f2=fopen("codes0", "wb+");
fwrite($f2, $s);
So the first thing you need to do is turn your single string into an array of two-character strings with str_split.
$hex_bytes = str_split($h, 2);
Then you want to convert each of those values from a hexadecimal string to the corresponding number with hexdec.
$code_array = array_map(hexdec, $hex_bytes);
Then you want the byte value corresponding to each of those character codes, which you can get with chr:
$char_array = array_map(chr, $code_array);
Finally, you want to join all those bytes together into a single string, which you can do with implode.
$s = implode($char_array);
You can use the steps above in that order, or you can put it all together into one expression like this:
$s = implode(array_map(chr, array_map(hexdec, str_split($h,2))));
Note that as soon as you get a value above 0x7F it's no longer "ASCII".
Assuming that array $binary is a previously constructed array bytes (like monochrome bitmap pixels in my case) that you want written to the disk in this exact order, the below code worked for me on an AMD 1055t running ubuntu server 10.04 LTS.
I iterated over every kind of answer I could find on the Net, checking the output (I used either shed or vi, like in this answer) to confirm the results.
<?php
$fp = fopen($base.".bin", "w");
$binout=Array();
for($idx=0; $idx < $stop; $idx=$idx+2 ){
if( array_key_exists($idx,$binary) )
fwrite($fp,pack( "n", $binary[$idx]<<8 | $binary[$idx+1]));
else {
echo "index $idx not found in array \$binary[], wtf?\n";
}
}
fclose($fp);
echo "Filename $base.bin had ".filesize($base.".bin")." bytes written\n";
?>
I have a binary file that is all 8 bit integers. I have tried to use the php unpack() functions but I cant get any of the arguments to work for 1 byte integers. I have tried to combine the data with a dummy byte so that I can use the 'n'/'v' arguments. I am working with a windows machine to do this. Ultimately I would like a function to return an array of integers based on a string of 8 bit binary integers. The code I have tried is below -
$dat_handle = "intergers.dat";
$dat_file = fopen($dat_handle, "rb");
$dat_data = fread($dat_file, 1);
$dummy = decbin(0);
$combined = $dummy.$dat_data;
$result = unpack("n", $combined);
What your looking for is the char datatype. Now there are two version of this, signed (lowercase c) and unsigned (uppercase C). Just use the one that's correct for your data.
<?php
$byte = unpack('c', $byte);
?>
Also, if the data file is just a bunch of bytes and nothing else, and you know it's length, you can do this. (If the length is 16 signed chars in a row.)
<?php
$bytes = unpack('c16', $byte);
?>
If you don't know how many bytes will be in the file, but you know there is only going to be bytes you can use the asterisk code to read until EOF.
<?php
$bytes = unpack('c*', $byte);
?>
The following should do what you want (ord):
$dat_handle = "intergers.dat";
$dat_file = fopen($dat_handle, "rb");
$dat_data = ord(fread($dat_file, 1));
What you are trying to do is retrieve the integer value of the single byte. Because you are reading in single bytes at a time, you will always have exactly one valid ASCII character. ord returns the binary value of that one character.
I have a device witch use binary format style config, and i have to generate that files on-the-fly.
File structure must consist of a number of configuration settings (1 per parameter) each of the form:
Type
Length
Value
where:
Type: is a single-octet identifier which defines the parameter
Length: is a single octet containing the length of the value field in octets (not including type and length fields)
Value: is from one to 254 octets containing the specific value for the parameter
I have a corresponding table
Type_code[int] => { Type_length[int] => Value[int/string/hex/etc.] }
How to parse that table to that binary format?
And, second way, how to parse that binary file, to php array format back?
There's the pack/unpack functions that can translate between various binary/hex/octal/string formats. Read a chunk of the file, extract necessary bits with unpack, and work from there.
$fh = fopen('data.txt', 'rb'); // b for binary-safe
// read 2 bytes, extract code/length, then read $length more bytes to get $value
while(($data = fread($fh, 2)) !== EOF)) {
list($code, $length) = unpack('CC', $data);
$data = fread($fh, $length);
// do stuff
}
fclose($fh);