PHP 5.2.17 - preg_replace_callback() doesn't work -


function:

function parse_csv($csv_string, $delimiter = ",", $skip_empty_lines = true, $trim_fields = true){     $enc = preg_replace('/(?<!")""/', '!!q!!', $csv_string);     $enc = preg_replace_callback(         '/"(.*?)"/s',         function ($field) {             return urlencode($field[1]);         },         $enc     );     $lines = explode("\n",$enc);     return array_map(         function ($line) use ($delimiter, $trim_fields) {             $fields = $trim_fields ? array_map('trim', explode($delimiter, $line)) : explode($delimiter, $line);             return array_map(                 function ($field) {                     return str_replace('!!q!!', '"', urldecode($field));                 },                 $fields             );         },         $lines     ); } 

works fine on php 5.3.x, in 5.2.17 getting error:

parse error: syntax error, unexpected t_function in /inc/func.php on line 6

why that? how fix that?

the function syntax you're using added in php 5.3. in earlier versions have use create_function.

return array_map(     create_function('$line', '         global $delimiter, $trim_fields;         $fields = $trim_fields ? array_map(\'trim\', explode($delimiter, $line)) : explode($delimiter, $line);         return array_map(             function ($field) {                 return str_replace(\'!!q!!\', \'"\', urldecode($field));             },             $fields         );',     $lines ); 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -