0

I have a semicolon delimited list that is exported to a csv file using fwrite and the shipping data is causing me some issues. The raw data in question looks like

Shipping (Interlink Express ( 20.88))

but this obviously breaks the layout due to the semicolon in the html entity. I thought I could fix this by replacing

$xml_orders .= $method.";";

with

$xml_orders .= html_entity_decode($method).";";

but that now gives me

Shipping (Interlink Express (€20.88))

I did try to html_entity_decode and then utf8_decode afterwards as I had previously used utf8_decode to fix a problem where ñ was being output as ñ, but that didn't fix the issue.

What is the best solution to this?

2
  • you can set the charset used by html_entity_decode using the 3rd parameter : see php.net/manual/fr/function.html-entity-decode.php Commented Jan 18, 2017 at 10:54
  • This will only “break”, if you tried to assemble the CSV string data yourself, and failed to take such special cases into account while doing so. The proper solution is to use the function PHP provides for this, fputcsv. Commented Jan 18, 2017 at 11:06

1 Answer 1

1

You need to escape the semicolon. The best way is to make every columun inside a double quotes, so the csv file can read as a single value. See the example below:

Col1;Col2;Col3;
Col1; "Col2 ; i need to escape it"; Col3;
Col1; Col2; "Col3 with escape of ; and ""quotes""";

For escape the double quotes inside a colum, you need to add two of ""

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.