Encode-Decode issue php (CSV->MySQL) - php

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"

Related

Read .txt file with 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

PHP Formatting A Column (dollar sign) In csv Upload Script

I have a script that uploads a csv file with 3 columns (phone, name, amount). The script removes any formatting on the phone, ie; ()- and puts the file on the server. The amount column in the file shows the amount like 125.00 and I need it to show $125.00. Any help would be much appreciated.
$file_destination = '/****/****/****/***/' . $file_name_new;
$contents = file_get_contents($file_tmp);
$contents = str_replace("(","",$contents);
$contents = str_replace(")","",$contents);
$contents = str_replace("-","",$contents);
file_put_contents($file_tmp, $contents);
if(move_uploaded_file($file_tmp, $file_destination)) {
Whether you want to reformat the values strictly for output or store the new formatted data in your CSV file, it's probably going to be more effective to use fgetcsv and fputcsv. Those functions are designed for properly reading in and writing out CSV formatted files.
Example
$rows = [];
if (($handle = fopen($file_tmp, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
list($phone, $name, $amount) = $data;
$phone = str_replace(['(',')','-'], '', $phone);
$amount = sprintf('$%.2f', $amount);
// you can build a new array with the updated values
$rows[] = [$phone, $name, $amount];
// or output directly
echo "$phone | $name | $amount";
}
fclose($handle);
}
// if you want to save the destination with the updated information...
$fp = fopen($file_destination, 'w');
foreach ($rows as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);

Updating CSV Column Values Using UA-Parser Library

I'm using the ua-parser library to identify the device family for a number of user agent strings in a spreadsheet column. The problem I'm running into is that it doesn't seem like my function is really running. The value output for detectAgent($data[2]) is not always accurate.
Here's a code sample. I feel like I must be missing something related to the limitations of creating objects over and over again.
Thanks in advance for any help.
<?php
require_once 'vendor/autoload.php';
use UAParser\Parser;
function detectAgent($ua) {
$parser = Parser::create();
$result = $parser->parse($ua);
return $result->os->family;
}
$input_file = "input.csv";
$output_file = "output.csv";
if (($handle1 = fopen($input_file, "r")) !== FALSE) {
if (($handle2 = fopen($output_file, "w")) !== FALSE) {
while (($data = fgetcsv($handle1, 5000000, ",")) !== FALSE) {
// Alter your data
#print $data . "<br />";
$data[2] = detectAgent($data[2]); //identify browser family
// Write back to CSV format
fputcsv($handle2, $data);
}
fclose($handle2);
}
fclose($handle1);
}
?>
This was a silly mistake. I was writing to the wrong column in $data[2] = detectAgent($data[2]);.
If anyone else runs into the same problem, the code is working now and I've posted an example here.

Parse large CSV into mysql db

I want to parse very large csv into mysql using php. My idea is to make it with executing 4-5 times the request which will paste data into mysql, after each iteration request should start from rows which has not been pasted yet (with one request iteration script can cover 400 rows). But I don't know to skip rows in csv, which has been already pasted into database. Maybe I should check number of rows in the table then define some variable with this number and make iteration according on it. But I don't know how to operate with csv using foreach, I only have code with "while" from example.
Here is my current code:
public function action_index(){
if(($handle = fopen('data_wpic.csv', 'r')) !== false)
{
$header = fgetcsv($handle);
while(($data = fgetcsv($handle)) !== false)
{
$model = ORM::factory('Drug');
$image_path = $data[18];
if(strlen($image_path) > 5) {
$path= 'drug_images/' . $image_path;
$image = ORM::factory('Image')->remote($path);
if ($image) {
$model->image_id = $image;
unlink($path);
}
}
$model->drugGenericName = $data[17];
$model->drugForm = $data[4];
$model->drugProperties = $data[7];
$model->drugIndication = $data[2];
$model->drugDosage = $data[13];
$model->drugSide = $data[11];
$model->drugContrIndication = $data[12];
$model->drugInteractions = $data[15];
$model->drugSpecial = $data[0];
$model->drugExpiry = $data[3];
$model->drugRealCondition = $data[8];
$model->tradeName = $data[16];
$model->save();
unset($data);
}
fclose($handle);
}
}
I have limited web hosting, thats why I try to solve this routine
If someone knows better aproach - I will be glad to hear him

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.

Categories