Howto remove html entity from the csv file while upload? - php

I'm trying to upload csv file, it uploads the csv, but the data are not correct.
How can I convert this into just string and remove the & lt;p&gt ; or the <p>?
Example of my csv file data:
**<p>\DECO APPLE GRN IN WOODEN CRATE SIZE 34X25X15.5CM PLASTIC</p>\**

You can do something like this.
$sample_string = '**<p>\DECO APPLE GRN IN WOODEN CRATE SIZE 34X25X15.5CM PLASTIC</p>\**';
echo strip_tags(html_entity_decode($sample_string));
// **\DECO APPLE GRN IN WOODEN CRATE SIZE 34X25X15.5CM PLASTIC\**
Application (if it's not malformed):
$data = array();
if (($handle = fopen('sample.csv', 'r')) !== false) {
while (($row = fgetcsv($handle, 4096, ',', "\n")) !== false) {
$data[] = array_map(function($var){
return strip_tags(html_entity_decode($var));
}, $row);
}
fclose($handle);
}
echo '<pre>';
print_r($data);
Output:
Array
(
[0] => Array
(
[0] => PROD.product_id
[1] => PROD.sync_2
[2] => PROD.quantity
[3] => PROD_DESC.name
[4] => PROD_DESC.description
[5] => PROD_DISC.price
)
[1] => Array
(
[0] => 1
[1] => ABCD
[2] => 2570
[3] => VIP
[4] => "\ DECO APPLE GRN IN WOODEN CRATE SIZE 34X25X15.5CM PLASTIC\"
[5] => 0.848
)
[2] => Array
(
[0] => 1
[1] => ABCDEFG
[2] => 2570
[3] => VIP 2
[4] => "\ DECO APPLE GRN IN WOODEN CRATE SIZE 34X25X15.5CM PLASTIC\"
[5] => 10
)
)

Related

Make product comparison table from the array

I have array like:
Array
(
[0] => Array
(
[product_id] => 19
[attributes] => Array
(
[0] => Array
(
[label] => Hard Disk
[name] => harddisk
[value] => 500 GB
)
[1] => Array
(
[label] => RAM
[name] => ram
[value] => 16 GB
)
[2] => Array
(
[label] => Graphics
[name] => graphics
[value] => 2 GB
)
[3] => Array
(
[label] => Graphics Card
[name] => graphicscard
[value] => 2 GB
)
)
)
[1] => Array
(
[product_id] => 20
[attributes] => Array
(
[0] => Array
(
[label] => Hard Disk
[name] => harddisk
[value] => 1 TB
)
[1] => Array
(
[label] => RAM
[name] => ram
[value] => 8 GB
)
[2] => Array
(
[label] => Graphics
[name] => graphics
[value] => 1 GB
)
[3] => Array
(
[label] => Graphics Card
[name] => graphicscard
[value] => 1 GB
)
)
)
)
I want to extract this array to tabular form as :
Product ID | 19 | 20
Hard Disk | 500 GB | 1 TB
RAM | 16 GB | 8 GB
Graphics | 2 GB | 1 GB
Graphics Card | 2 GB | 1 GB
There may come another product id also as it is dynamic. I am able to get first column but how to get other column. My code is:
<table class="table">
<?php
$count = count($all);
if (!empty($all)) {
foreach ($all as $value) {
?>
<?php foreach ($value['attributes'] as $val) { ?>
<tr>
<td><?php echo $val['label']; ?></td>
</tr>
<?php
} exit();
}
}
?>
</table>
I'm always completely baffled by the mixing of HTML and PHP. I prefer a clearer look.
I would solve your problem in two steps:
1: Turn your array into an array that can easily be rendered as a table.
2: Render the table from that array.
if (is_array($all)) {
// step 1: create table
foreach ($all as $one) {
$id = $one['product_id'];
$table['Product Id'][$id] = $id;
foreach ($one['attributes'] as $attribute) {
$label = $attribute['label'];
$value = $attribute['value'];
$table[$label][$id] = $value;
}
}
// step 2: render the table
echo '<table class="table">'.PHP_EOL;
foreach ($table as $label => $row) {
echo "<tr><td>$label</td>";
foreach ($table['Product Id'] as $id) {
echo '<td>'.(isset($row[$id]) ? $row[$id] : '-').'</td>';
}
echo '</tr>'.PHP_EOL;
}
echo '</table>'.PHP_EOL;
}
This routine can cope with missing attributes.
This code is not perfect, but it gives you a point to start. The challenge is to reduce the complexity even more. I think that's possible.

Fetching data error from json format

I have the following JSON format
Array (
[version] => 3
[status] => ok
[response] => Array (
[data] => Array (
[0] => Array (
[accessible_wheelchair] => 1
[address] => 200 5th Ave
[category_labels] => Array (
[0] => Array (
[0] => Social
[1] => Food and Dining
[2] => Restaurants
[3] => Italian
)
[1] => Array (
[0] => Social
[1] => Food and Dining
[2] => Restaurants
[3] => Steakhouses
)
[2] => Array (
[0] => Social
[1] => Entertainment
[2] => Music and Show Venues
)
)
[country] => us
)
)
[included_rows] => 20
)
)
I want to fetch the data from the data->[0] array.
I used the following code to insert the data into database
$newurl="/*the api link is here*/";
$json = file_get_contents($newurl);
$data = json_decode($json,true);
foreach($data as $value){
$value1=$value;//$value
foreach($value as $newValue){
foreach($newValue as $newValue1){
$name=$newValue1["name"];
$sql="insert into restaurant (name) values(".$name.");";
db_query($sql) or die("insert error");
}
}
}
But it cannot be able to insert the data.Please whether there is any other method to fetch the my desired data and store it in database.
where is restaurant name?
<?php
$newurl="/*the api link is here*/";
$json = file_get_contents($newurl);
if ($json)
{
if (!($response = json_decode(stripslashes($json), TRUE)))
{
die("invalid json format");
}
}
if (!empty($response['status']) && $response['status'] == "ok")
{
$data = $response['response']['data'];
for ($i=0; $i < count($data); $i++)
{
// in this step data 0 wich you want to extract
/*
[accessible_wheelchair] => 1
[address] => 200 5th Ave
[category_labels] => Array (
[0] => Array (
[0] => Social
[1] => Food and Dining
[2] => Restaurants
[3] => Italian
)
[1] => Array (
[0] => Social
[1] => Food and Dining
[2] => Restaurants
[3] => Steakhouses
)
[2] => Array (
[0] => Social
[1] => Entertainment
[2] => Music and Show Venues
)
)
*/
// but what i confused here is restaurant name? i don't see restaurant name here..
}
}

json to csv conversion in php

i am getting a json response from a site,
then convert it into json decoded file, my code is
$author = $_POST['author_name'];
$author_name = str_replace(' ', '+', $author);
$term = $_POST['term'];
$term_search = str_replace(' ', '+', $term);
$country = $_POST['country'];
$entity = 'ebook';
$search_url = "https://itunes.apple.com/searchterm=".$term_search."&entity=".$entity."&country=".$country;
$data = file_get_contents($search_url);
$josn_decoded = json_decode($data, true);
$file_name = "searched_book.csv";
$fp = fopen($file_name, 'w');
foreach($josn_decoded['results'] as $search_result){
fputcsv($fp, $search_result);
}
fclose($fp);
the response of echo "<pre>"; print_r($josn_decoded) is like
<pre>Array
(
[resultCount] => 5
[results] => Array
(
[0] => Array
(
[artistId] => 673560392
[artistName] => Hanna Raskin
[kind] => ebook
[price] => 2.99
[description] => Yelp Help, written by professional food critic Hanna Raskin (Seattle Weekly, Dallas Observer).
[currency] => USD
[genres] => Array
(
[0] => Cookbooks, Food & Wine
[1] => Books
[2] => Computers & Internet
[3] => Internet
)
[genreIds] => Array
(
[0] => 9028
[1] => 38
[2] => 9027
[3] => 10020
)
[releaseDate] => 2013-07-06T07:00:00Z
[trackId] => 673560390
[trackName] => Yelp Help: How to Write Great Online Restaurant Reviews
[formattedPrice] => $2.99
[artistIds] => Array
(
[0] => 673560392
)
[artworkUrl60] => http://a2.mzstatic.com/us/r30/Publication/v4/30/fa/89/30fa8929-ba32-41fd-c046-bb660b2c886c/9781301327515.60x60-50.jpg
[artistViewUrl] => https://itunes.apple.com/us/artist/hanna-raskin/id673560392?mt=11&uo=4
[trackCensoredName] => Yelp Help: How to Write Great Online Restaurant Reviews
[fileSizeBytes] => 219793
[artworkUrl100] => http://a5.mzstatic.com/us/r30/Publication/v4/30/fa/89/30fa8929-ba32-41fd-c046-bb660b2c886c/9781301327515.100x100-75.jpg
[trackViewUrl] => https://itunes.apple.com/us/book/yelp-help-how-to-write-great/id673560390?mt=11&uo=4
)
[1] => Array
(
[artistId] => 413390948
[artistName] => Gradiva Couzin & Jennifer Grappone
[kind] => ebook
[price] => 1.99
[description] => While most businesses know the importance of online reviews on sites such as Yelp.com, they have no clue how to grab the reins and help shape the conversation around their service or product.
[currency] => USD
[genres] => Array
(
[0] => Industries & Professions
[1] => Books
[2] => Business & Personal Finance
)
[genreIds] => Array
(
[0] => 10005
[1] => 38
[2] => 9009
)
[releaseDate] => 2013-10-03T07:00:00Z
[trackId] => 737317739
[trackName] => Yelp for Business
[formattedPrice] => $1.99
[artistIds] => Array
(
[0] => 413390948
[1] => 413390943
)
[artworkUrl60] => http://a2.mzstatic.com/us/r30/Publication6/v4/48/d4/fe/48d4fe78-2668-0c25-02f2-0e1cf2c21983/9781118857731.60x60-50.jpg
[artistViewUrl] => https://itunes.apple.com/us/artist/gradiva-couzin-jennifer-grappone/id413390948?mt=11&uo=4
[trackCensoredName] => Yelp for Business
[fileSizeBytes] => 2703426
[artworkUrl100] => http://a4.mzstatic.com/us/r30/Publication6/v4/48/d4/fe/48d4fe78-2668-0c25-02f2-0e1cf2c21983/9781118857731.100x100-75.jpg
[trackViewUrl] => https://itunes.apple.com/us/book/yelp-for-business/id737317739?mt=11&uo=4
)
)
)
when i write it into csv file i get following error
Array to string conversion in D:\wamp\www\search_book\search_code.php on line 29
please guide me.. i am new to this
fputcsv only accept one dimension array, not multidimensional array. You have to convert the genres, genreIds, artistIds, etc into string. Maybe you need something like implode() method.
For example:
foreach($josn_decoded['results'] as $search_result) {
if (is_array($search_result)) {
$search_result = implode('|', $search_result);
}
fputcsv($fp, $search_result);
}
So in csv, your genres column be like Cookbooks, Food & Wine|Books|Business & Personal Finance.
Try this one :)
<?php
$json_str = "{'aintlist':[4,3,2,1], 'astringlist':['str1','str2']}";
$json_obj = json_decode ($json_str);
$fp = fopen('file.csv', 'w');
foreach ($json_obj as $fields)
{
fputcsv($fp, $fields);
}
fclose($fp);
?>

csv column data value that breaks PHP code

I have a CSV file that has 2 rows like that :
11,"HUFFY SPORTS DELAWARE","INC.","HUNTINGTON NATIONAL BANK","DEPT. L-2493","COLUMBUS","OH","","","43260-2493","/\ /\ /\/"
12,"OXFORD GOLF","12564 COLLECTIONS CTR DR","","CHICAGO","IL","","",60693,"","912-526-1100",""
I'm reading that file in PHP and populating an array with each data column using the following code :
<?php
$values = array();
$csvFileName = "test.csv";
if (($handle = fopen($csvFileName, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(count($data) == 1){
$f = fopen('php://temp', 'w');
fwrite($f, $data[0]);
rewind($f);
$data = fgetcsv($f, 1000, ",");
fclose($f);
}
for($i=0; $i<sizeof($data); $i++){
$values[$i] = $data[$i];
}
echo "<pre>";
print_r($values);
echo "</pre>";
}
}
?>
so I should get a result like that :
Array
(
[0] => 11
[1] => HUFFY SPORTS DELAWARE
[2] => INC.
[3] => HUNTINGTON NATIONAL BANK
[4] => DEPT. L-2493
[5] => COLUMBUS
[6] => OH
[7] =>
[8] =>
[9] => 43260-2493
[10] => /\ /\ /\
)
Array
(
[0] => 12
[1] => OXFORD GOLF
[2] => 12564 COLLECTIONS CTR DR
[3] =>
[4] => CHICAGO
[5] => IL
[6] =>
[7] =>
[8] => 60693
[9] =>
[10] => 912-526-1100
[11] =>
)
however this string "/\ /\ /\" is breaking my code (sounds like the last double quote is ignored) and I get the first line a column data like the following :
Array
(
[0] => 11,"HUFFY SPORTS DELAWARE","INC.","HUNTINGTON NATIONAL BANK","DEPT. L-2493","COLUMBUS","OH","","","43260-2493","/\ /\ /\""
12
[1] => OXFORD GOLF""
[2] => 12564 COLLECTIONS CTR DR""
[3] => "
[4] => CHICAGO""
[5] => IL""
[6] => "
[7] => "
[8] => 60693
[9] => "
[10] => 912-526-1100""
[11] => ""
)
Is there a way to fix this please?
Set the $escape parameter of the fgetcsv() function to an empty string unless you are actually escaping " with a backslash:
fgetcsv($handle, 1000, ",", '"', '')

php function fgetcsv not work for line have \""

I have data in phpmyadmin, example: (first row is header)
||from||to||messages||
||john||andy||meeting now||
||dony||sherly||Place in \"Jakarta\" , Indonesia, 15412
when I export to csv via phpmyadmin version 3.5.6, I get this csv file:
"from","to","messages"
"john","andy","meeting now"
"from","to","Place in \""Jakarta\"" , Indonesia, 15412"
if I use this code
<?php
// code start
$file = fopen($path, 'r');
while (($line = fgetcsv($file)) !== FALSE) {
print_r($line);
}
fclose($file);
// code end
?>
I hope get this
//output start
Array
(
[0] => from
[1] => to
[2] => messages
)
Array
(
[0] => john
[1] => andy
[2] => meeting now
)
Array
(
[0] => dony
[1] => sherly
[2] => Place in \"Jakarta\" , Indonesia, 15412
)
//output end
but instead I get
//output start
Array
(
[0] => from
[1] => to
[2] => messages
)
Array
(
[0] => john
[1] => andy
[2] => meeting now
)
Array
(
[0] => from
[1] => to
[2] => Place in \"Jakarta\""
[3] => Indonesia
[4] => 15412"
)
//output end
anyone can help this error?
You have double double quotes.
"from","to","Place in \""Jakarta\"" , Indonesia, 15412"
should be
"from","to","Place in \"Jakarta\" , Indonesia, 15412"

Categories