PHP Function str_getcsv() not working

Posted on: July 19th, 2015 by BuildMyWeb No Comments

It is a common practice to develop new sites or significant changes to an existing website on a development server. You do your rockstar work, follow it up with some stellar testing and debugging, and then,… when completely satisfied with your results,… you publish your new files to your production server. And voila! Something breaks down.

I ran into this issue recently and for some wonderfully delicious reason, my mind immediately went to ‘check php versions’. Sure enough, my development server is running PHP v5.3 and my production server is running v5.2. The reality is, I was about to flip the site mode switch back into ‘dev’ mode so I would have seen the error that was thrown and realized str_getcsv() was not being supported. But it’s all the more satisfying that I figured it out on my own.

Organize a settings.php File for Site-wide Variables and Constants

See, I keep a settings.php file in all of my productions. I keep many global variables and CONSTANTS there for easy reference. One such setting I have is a SITE_MODE constant. I have lots of conditionals that say for instance,

if( SITE_MODE == 'dev' ){ ADMIN_EMAIL = '; }

. All of my scripts utilize ADMIN_EMAIL instead of hardcoding email addressed. This way I can use my own email address when developing a project to receive notification emails and quickly edit one line of code when ready for the client email address to receive notifications, in LIVE mode. I also keep database values stored here, configure timezones for date functions, etc. But I digress…

“Call to undefined function str_getcsv()”

When I put my new site into ‘dev’ mode, I received the error: ‘Call to undefined function str_getcsv()’. I was attempting to read the contents of a text file and split the string into an array. But, as you can guess, the str_getcsv function was not working. I could make a case with my hosting provider to upgrade my server’s PHP version. Or some hosts allow you to switch between a few versions yourself. My client’s host is GoDaddy however. Yes, holy mother of all that is foul and intolerable, I would have to deal with GoDaddy. Rather than suffer through that process, I chose to look for an alternative. I did a quick Google search and came across this little golden nugget:

// if php version < 5.3, use our custom str_getcsv()
if( !function_exists('str_getcsv') ) 
    	function str_getcsv($input, $delimiter = ',', $enclosure = '"') 
        	if( ! preg_match("/[$enclosure]/", $input) ) 
		{ return (array)preg_replace(array("/^\\s*/", "/\\s*$/"), '', explode($delimiter, $input)); }
        	$token = "##"; $token2 = "::";
        	//alternate tokens "\034\034", "\035\035", "%%";
        	$t1 = preg_replace(array("/\\\[$enclosure]/", "/$enclosure{2}/",
                "/[$enclosure]\\s*[$delimiter]\\s*[$enclosure]\\s*/", "/\\s*[$enclosure]\\s*/"),
                array($token2, $token2, $token, $token), trim(trim(trim($input), $enclosure)));

        	$a = explode($token, $t1);
        	foreach($a as $k=>$v) 
            	    if ( preg_match("/^{$delimiter}/", $v) || preg_match("/{$delimiter}$/", $v) ) 
		    { $a[$k] = trim($v, $delimiter); $a[$k] = preg_replace("/$delimiter/", "$token", $a[$k]); }
        	$a = explode($token, implode($token, $a));
        	return (array)preg_replace(array("/^\\s/", "/\\s$/", "/$token2/"), array('', '', $enclosure), $a);

In essence, it runs a conditional to see if the function str_getcsv() exists. If it does not, it declares the custom one above. I’ve used it and it worked perfectly. It was a stackoverflow thread that directed me to the above:


Leave a Reply