Weird characters when parsing CSV in PHP

For some reason, if you do this PHP

$csv_file = file_get_contents(site_url($file_name));
$csv_lines = explode(“n”, $csv_file);

a CSV line like this

Campaign Name    Ad Group Name    Component Type    …

turns into a weird line like this:

377376C^@a^@m^@p^@a^@i^@g^@n ^@N…

This has to do with PHP’s character encoding issues.

Here’s the easier way to take care of this problem:

$csv1 = file($csv_file);
$csv2 = implode($csv1);
$csv3 = iconv(“UTF-16″,”ISO-8859-1″,$csv2);
$csv = explode (“n”,$csv3);

This will solve that character issue.

Related posts:

  1. Geek talk gone dirty

Leave a Reply

Your email address will not be published. Required fields are marked *

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>