Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP 5.2.17-preg_replace_callback()不';行不通_Php - Fatal编程技术网

PHP 5.2.17-preg_replace_callback()不';行不通

PHP 5.2.17-preg_replace_callback()不';行不通,php,Php,功能: 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) {

功能:

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
    );
}
函数parse_csv($csv_string,$delimiter=“,”,$skip_empty_lines=true,$trim_fields=true){

$enc=preg_replace('/(?此问题的解决方案已发布在此处:

下面是使用PHP 5.2或更早版本的preg_replace_回调的建议解决方案示例:

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

请注意,在5.3之前的PHP版本中,匿名函数/闭包不可用。但是您仍然可以使用
preg\u replace\u callback()
。下面是(示例2)中的一个示例:


您使用的
函数
语法是在PHP5.3中添加的。在早期版本中,您必须使用
创建函数

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

闭包从PHP5.3开始就可以使用。我不知道为什么在PHP5.1中可以使用闭包。对不起,我的不好…似乎它在5.3中可以使用。。。
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
);