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?

2
  • 2
    Closures are available as of PHP 5.3. I don't know why this would work in PHP 5.1 php.net/manual/en/class.closure.php Commented Apr 24, 2014 at 15:01
  • Sorry my bad... seems that it worked on 5.3... Commented Apr 24, 2014 at 15:07

3 Answers 3

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
);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.