1

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, but in 5.2.17 I am getting an error:

Parse error: syntax error, unexpected T_FUNCTION in /inc/func.php on line 6

Why is that? How to fix that?

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91

3 Answers3

2

The function syntax you're using was added in PHP 5.3. In earlier versions you have to 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
);
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Note that in versions of PHP prior to 5.3, anonymous functions/closures were not available. However you can still use preg_replace_callback(). Here's an example from the docs (example #2):

<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// the callback function
function next_year($matches)
{
  // as usual: $matches[0] is the complete match
  // $matches[1] the match for the first subpattern
  // enclosed in '(...)' and so on
  return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
            "|(\d{2}/\d{2}/)(\d{4})|",
            "next_year",
            $text);

Note that the function next_year is defined in the normal way, and then passed to preg_replace_callback() as a string.

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48
0

The solution to this problem has been posted here: Parse error: syntax error, unexpected T_FUNCTION line 10 ? help?

Here is an example of the proposed solution for using preg_replace_callback with PHP 5.2 or earlier:

class My_callback {
  public function __construct($s, $r) {
    $this->s = $s; $this->r = $r;
  } 

  function callback($v) { return $this->r[$v[1]]; }
}

function search_replace($s,$r,$sql) {
  $e = '/('.implode('|',array_map('preg_quote', $s)).')/';
  $r = array_combine($s,$r);
  $c = new My_callback($s, $r);
  return preg_replace_callback($e, array($c, 'callback'), $sql);
}
Community
  • 1
  • 1
clami219
  • 2,958
  • 1
  • 31
  • 45