TransWikia.com

Problem with quotation marks on PHP export

Magento Asked by Stefan Schulte on November 13, 2021

I could use your help once again… I’m currently building a php export to hand our products off to a comparison website. One of their requirements is that every string is wrapped in double quotes.

I’ve tried different things, but I’m still running into problems here. I don’t know why it’s doing what it’s doing and am open to any suggestions y’all might have!

First thing I tried was simply putting

$art_nr = '"'.$product->getSku().'"';

And then I tried using the strip method to ensure there aren’t any quotes already around the string…

$art_farbe = '"'.trim($art_farbe,'"').'"';

But the result is always the same and looks like this:

art_nr|art_name|art_beschreibung|art_url|art_img_url|art_img_url2|art_img_url3|art_img_url4|art_img_url5|art_img_url6|art_img_url7|art_img_url8|art_img_url9|art_img_url10|art_material|art_waehrung|art_preis|art_lieferkosten|art_stamm|art_farbe|art_breite|art_breite_einheit|art_laenge|art_laenge_einheit|art_hoehe|art_hoehe_einheit|art_sitzhoehe|art_sitzhoehe_einheit
"""jkfalieg-xxl"""|"Fiam Amigo XXL"|"XXL Liege mit Gestell aus Aluminium. Bezug: Textiline (wetterfest und uv-beständig)., "|"""https://www.zeitlos-einrichtung.info/fiam-amigo-xxl.html"""|"""https://www.zeitlos-einrichtung.info/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/f/i/fiamamigo.jpg"""|https://www.zeitlos-einrichtung.info/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/f/i/fiamamigo.jpg|||||||||"""Aluminium"""|"""EUR"""|"""195.0000"""|"""0"""||""""""|"""75"""|"""cm"""|"""207"""|"""cm"""|"""33"""|"""cm"""|""""""|""""""

For everything I use one of the two methods mentioned above I get three double quotes in front and after the content of the string. The second and third column are working because I didn’t to anything to them since they’re automatically surrounded by double quotes since there’s spaces in the string.

If someone has an idea how to fix this please let me know! 🙂

2 Answers

I would recommend working with php native functions when building csv files. For example fputcsv would probably work for you. In this case you can define:

  1. The delimiter used in the csv (in your case |)
  2. What to enclose each entry with (in your case ")

Direct example from php.net

Example #1 fputcsv() example

<?php

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>

The above example will write the following to file.csv:

aaa,bbb,ccc,dddd
123,456,789
"""aaa""","""bbb"""

Answered by David Manners on November 13, 2021

@7ochem is probably the one to ask as to how to go about this with Magento's built in methods for exporting a collection of varien objects to CSV, however, you are in a good place with the code you have got and the underlying PHP csv functions of Magento's methods do not allow for all quotes around everything.

I suggest that you stick with arrays and once you have a 'row' iterate over it once to replace existing " marks with single quotes. This will work fine and should not matter for the porpoises of a comparison shopping site (except if you are using customary units for dimensions).

Now 'implode' your array 'row' with "|" and bolt on a " at the front and end. You should now have something clean with no double-escaped out double quotes there to break anything.

Answered by Henry's Cat on November 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP