Read .txt file with PHP - php

using php, I have to read a txt file (UTF-8 format) in which there are values to be inserted into a mysql db, based on character length.
This is my script:
if (($handle = fopen(DIRECTORY_FILE.'test.txt', "r")) !== FALSE) {
while ($data = fgets($handle)) {
$mod = (int)substr($data, 0,7);
$cod = (int)substr($data, 10,3);
$descr = trim(substr($data,13,255));
$descr2 = trim(substr($data,268,255));
$seq = (int)substr($data, 523,4);
$codord = (int)substr($data, 527,20);
}
fclose($handle);
}
This works well until there are no special characters.
I noticed that when there is a degree symbol " ° ", is counted as 2 length, causing an error in the reading of the txt.
How can I ensure that the count is correct?
Thanks

Related

Encode-Decode issue php (CSV->MySQL)

I'm trying to pull data in CSV format for a server, process and store in a MySQL database:
When I insert into my database, the text is "Atila%27s+Goonies" I want it to be "Atila's Goonies"
I have been reading through all the text encoding topics, but I think I'm missing a very simple function.
My requirement is to have this text and store in the database as a properly formatted (or encoded) string, so I can query later and display online. Database column is collated as "utf8_unicode_ci". I'm not sure if it's relevant because if I want to display on the browser, I cannot get to the correct format either. thanks.
Any help would be appreciated.
My code below:
if (($handle = fopen("alliances.txt", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
$alliance_id = $data[0];
$alliance_name = $data[1];
$alliance_points = $data[2];
$alliance_villages = $data[3];
$alliance_members = $data[4];
$alliance_rank = $data[5];
$sql_ia = "insert into alliances (alliance_id,alliance_name, alliance_villages,alliance_members,alliance_rank,timestamp)
values (:alliance_id,:alliance_name, :alliance_villages,:alliance_members,:alliance_rank,:timestamp)";
$st_ia = $DBcon->prepare($sql_ia);
$st_ia->bindParam(':alliance_rank', $alliance_rank,PDO::PARAM_INT);
$st_ia->bindParam(':alliance_id', $alliance_id,PDO::PARAM_INT);
$st_ia->bindParam(':alliance_name',$alliance_name,PDO::PARAM_STR);
$st_ia->bindParam(':alliance_members',$alliance_members,PDO::PARAM_INT);
$st_ia->bindParam(':alliance_villages',$alliance_villages,PDO::PARAM_INT);
$st_ia->bindParam(':timestamp',$timestamp,PDO::PARAM_INT);
$author_name = $ag_row[$agency];
$st_ia->execute();
}
fclose($handle);
}
Try this.
<?php
$encoded = 'Atila%27s+Goonies';
// Your string is URL encoded. Use urldecode() to decode.
$decoded = urldecode($encoded);
var_dump($decoded); // string(15) "Atila's Goonies"

Automating CSV conversion from iso-8859-2 to utf-8

I have quite a few CSV files that are unfortunately encoded with iso-8859-2 (according to Brackets). I would like to iterate over these files with PHP and convert them.
I found https://csv.thephpleague.com/9.0/converter/charset/ but the way I can use the conversion function is uncertain to me.
Their example code
use League\Csv\CharsetConverter;
$csv = new SplFileObject('/path/to/french.csv', 'r');
$csv->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY);
$encoder = (new CharsetConverter())->inputEncoding('iso-8859-15');
$records = $encoder->convert($csv);
This is my code so far that is part of a form to upload one file and save the contents to the database for testing. It of course saves the text in the incorrect format.
$db = ConnectDB::getConnection('address_dtb');
$sql = " ... ";
$stmt = $db->prepare($sql);
$rowCount = 0;
$temp_name = $_FILES['adresscsv']['tmp_name'];
$file_handle = fopen($temp_name, 'r');
while (($items = fgetcsv($file_handle, 1000, ';')) !== FALSE) {
if($flag) { $flag = false; continue; }
$stmt->execute($items);
$rowCount++;
}
fclose($file_handle);
ConnectDB::closeConnection($db);
What is the correct way to use the PHP CSV library above to iterate over locally saved files in a for loop to automate the process?
I ended up using iconv as hinted.
$files = glob('address/*.csv');
foreach ($files as $csv) {
$file_data = file_get_contents($csv);
$utf8_file_data = iconv('Windows-1250', 'UTF-8', $file_data);
file_put_contents($csv, $utf8_file_data);
}
You do not have to use a library. There is a function in PHP that can do that iconv

Convert a CSV file to force encapsilation

My ideal fix would be a function that can take a CSV file that does not have forced encapsulation (no quotes around values if the value has no spaces or is just a number) and convert it into a CSV file that makes sure every field is encapsulated with double quotes.
<?php
$raw_file = BASE_DIR."pathto/csv.csv";
$fixed_file = BASE_DIR."pathto/fixed.csv";
convert_file($raw_file, $fixed_file);
//move on with life!!
?>
Thanks for you help!
Use fgetcsv to get the contents of your original csv file and fputcsv (using the fourth parameter) to build the encapsulated file.
For example, supposing your column separator is ; :
<?php
$raw_file = BASE_DIR."pathto/csv.csv";
$fixed_file = BASE_DIR."pathto/fixed.csv";
// Getting contents
$raw_handle = fopen($raw_file, 'r');
$contents = array();
while (($data = fgetcsv($raw_handle, 0, ';')) !== false) {
$contents[] = $data;
}
fclose($raw_handle);
// Putting contents
$fixed_handle = fopen($fixed_file, 'w');
foreach ($contents as $line) {
fputcsv($fixed_handle, $line, ';', '"');
}
fclose($fixed_handle);
//move on with life!!
?>

Php script to Import CSV with Arabic content

I have to build a script to import a CSV file, (With Arabic content in one coloumn).
I have been searching online and manage to convert my CSV file to UTF8
$filename = $_FILES["csv"]["tmp_name"];
$file = fopen($filename, "r");
$count = 0;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$count++;
if($count>1){
// tried to convert using below
$string_decoded = iconv("windows-1256", "utf-8", $emapData[2]);
// convert code above
$sql = "INSERT into `$table` (SurahNo,AyatNo,Ayat) values ('$emapData[0]','$emapData[1]','$string_decoded')";
echo $sql;
mysql_query($sql);
}
}
I tried to convert using iconv but still getting garbage content imported in mysql.
Imported content look like this
ط¨ظگط³ظ’ظ…ظگ ط§ظ„ظ„ظ‘ظ
Can share my CSV file if you wish.
Need help, spend hours searching.

CSV file read fail (PHP )

I am trying to read a CSV file (delimited by commas) but unfortunately, it isn't responding as it ought to. I am not so sure what I am doing wrong here, but I'll paste out the contents of the code and the CSV file both :
$row = 0;
if($handle = fopen("SampleQuizData.csv","r") !== FALSE)
{
// WORKS UNTIL HERE, SO FILE IS BEING READ
while(!feof(handle))
{
$line = fgetcsv($handle, 1024, ",") ;
echo $line[2]; // DOES NOT WORK
}
}
Here is the CSV file: (the emails and names have been changed here to protect the identities of the users)
parijat,something,parijatYkalia#hotmail.com
matthew,durp, mdurpdurp#gmail.com
steve,vai,stevevai#gmail.com
rajni,kanth,rajnikanth#superman.com
it lacks a '$' to the handle variable
while(!feof($handle)){
and not :
while(!feof(handle)){
Give this a try:
<?php
$row = 0;
if (($handle = fopen("SampleQuizData.csv", "r")) !== FALSE)
{
while(!feof($handle))
{
$line = fgetcsv($handle, 1024, ",") ;
echo "$line[2]";
}
}
?>
It's worth a mention but when I was working on CSV exports a few weeks ago, I had weird line ending inconsistencies. So I put this at the top of my php file and it worked splendid.
<?php
ini_set("auto_detect_line_endings", true);
?>

Categories