<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ivan Villareal &#187; BOM problems when reading files with PHP &#8211; Ivan Villareal</title>
	<atom:link href="http://ivanvillareal.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://ivanvillareal.com</link>
	<description>IT stuff and more...</description>
	<lastBuildDate>Tue, 01 Nov 2011 23:00:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>BOM problems when reading files with PHP</title>
		<link>http://ivanvillareal.com/uncategorized/bom-problems-when-reading-files-with-php/</link>
		<comments>http://ivanvillareal.com/uncategorized/bom-problems-when-reading-files-with-php/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 23:34:44 +0000</pubDate>
		<dc:creator>Ivan Villareal</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ivanvillareal.com/?p=85</guid>
		<description><![CDATA[Today I was developing a script that was going to get some data from an uploaded file to start a batch job, I did the front end with the help [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was developing a script that was going to get some data from an uploaded file to start a batch job, I did the front end with the help of &nbsp;<a href="http://www.uploadify.com/" target="_blank">uploadify</a>&nbsp;and everything was ok, I promptly did a php script to get the data.</p>
<p>I&#8217;m using the <a title="file — Reads entire file into an array" href="http://php.net/manual/en/function.file.php" target="_blank">file()</a> function, (I&#8217;m lazy I know), It was working fine, I did a test case and was displaying the output to the console, everything was working as expected, I&#8217;ve played a little with the flags and did a couple of validations to handle Windows, MAC and Unix line endings, and it was good, so I moved on to the model part, and after my first test inserting some data to the db I had a very weird problem.</p>
<p>The first line, was&nbsp;definitely&nbsp;something wrong, I wasn&#8217;t able to see any weird&nbsp;character&nbsp;from the mysql console, but I&#8217;ve noticed some strange&nbsp;character&nbsp;when i viewed with mysql browser, whenever I printed the result seemed ok, I&#8217;ve also did some debug on the queries and it was ok, but in the mysql query browser this weird square was showing for the first row.</p>
<p>I&nbsp;thought&nbsp;that this was an encoding issue, so I&#8217;ve started looking at this, and what I&#8217;ve learned Today was that some editors, add this apparently useless Byte Order Mark to the beginning of UTF encoded files, this are the bytes added 0xEF,0xBB,0xBF.</p>
<p>I&#8217;ve spent&nbsp;almost&nbsp;an hour trying to get a&nbsp;workaround&nbsp;for this, so after some testing this was my solution to get this done:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">            <span style="color: #000088;">$data</span><span style="color: #339933;">=</span> <span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullFileName</span><span style="color: #339933;">,</span> FILE_IGNORE_NEW_LINES <span style="color: #339933;">|</span> FILE_SKIP_EMPTY_LINES <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//Remove BOM</span>
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #990000;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CCC&quot;</span><span style="color: #339933;">,</span><span style="color: #208080;">0xef</span><span style="color: #339933;">,</span><span style="color: #208080;">0xbb</span><span style="color: #339933;">,</span><span style="color: #208080;">0xbf</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">processData</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;ve tested this with ANSI, UTF-8 encodings and with Windows, Unix, and Mac CR endings, and it works, I think that there might be a better solution, but this has taken me too much time Today.</p>
]]></content:encoded>
			<wfw:commentRss>http://ivanvillareal.com/uncategorized/bom-problems-when-reading-files-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

