Php phát nổ dấu phẩy không có trong dấu ngoặc kép

@normadize - that is a nice start, but it fails on situations where a field is empty but quoted (returning a string with one double quote instead of an empty string) and cases like """""foo""""" that should result in ""foo"" but instead return "foo". I also get a row with 1 empty field at the end because of the final CRLF in the CSV. Plus, I don't really like the !!Q!! magic or urlencoding to get around things. Also, \R doesn't work in pcre on any of my php installations.

Here is my take on this, without anonymous functions (so it works on PHP < 5.3), and without your options (because I believe the only correct way to parse according to the RFC would be $skip_empty_lines = false and $trim_fields = false).

//parse a CSV file into a two-dimensional array
//this seems as simple as splitting a string by lines and commas, but this only works if tricks are performed
//to ensure that you do NOT split on lines and commas that are inside of double quotes.
function parse_csv($str)
{
    //match all the non-quoted text and one series of quoted text (or the end of the string)
    //each group of matches will be parsed with the callback, with $matches[1] containing all the non-quoted text,
    //and $matches[3] containing everything inside the quotes
    $str = preg_replace_callback('/([^"]*)("((""|[^"])*)"|$)/s', 'parse_csv_quotes', $str);

    //remove the very last newline to prevent a 0-field array for the last line
    $str = preg_replace('/\n$/', '', $str);

    //split on LF and parse each line with a callback
    return array_map('parse_csv_line', explode("\n", $str));
}

//replace all the csv-special characters inside double quotes with markers using an escape sequence
function parse_csv_quotes($matches)
{
    //anything inside the quotes that might be used to split the string into lines and fields later,
    //needs to be quoted. The only character we can guarantee as safe to use, because it will never appear in the unquoted text, is a CR
    //So we're going to use CR as a marker to make escape sequences for CR, LF, Quotes, and Commas.
    $str = str_replace("\r", "\rR", $matches[3]);
    $str = str_replace("\n", "\rN", $str);
    $str = str_replace('""', "\rQ", $str);
    $str = str_replace(',', "\rC", $str);

    //The unquoted text is where commas and newlines are allowed, and where the splits will happen
    //We're going to remove all CRs from the unquoted text, by normalizing all line endings to just LF
    //This ensures us that the only place CR is used, is as the escape sequences for quoted text
    return preg_replace('/\r\n?/', "\n", $matches[1]) . $str;
}

________số 8

//restore any csv-special characters that are part of the data
function parse_csv_field($field) {
    $field = str_replace("\rC", ',', $field);
    $field = str_replace("\rQ", '"', $field);
    $field = str_replace("\rN", "\n", $field);
    $field = str_replace("\rR", "\r", $field);
    return $field;
}

Đôi khi chúng ta cần thêm dấu ngoặc kép vào các giá trị trong một chuỗi được phân tách bằng dấu phẩy. Ví dụ: khi sử dụng tìm kiếm FULLTEXT của MySQL, chúng ta cần sử dụng một số loại chuỗi như 'pasta', 'tun', 'Eggs' HOẶC tìm danh sách lớn các Id từ MySQL chẳng hạn như WHERE item IN ('pasta', 'tun', . Ở đây trong bài đăng này, bạn đã chạy mã PHP để thêm dấu ngoặc kép vào các giá trị trong một chuỗi được phân tách bằng dấu phẩy bằng cách sử dụng PHP

Ngoài ra, đọc

  • Hơn 15 biểu thức chính quy dành cho nhà phát triển PHP
  • Chuyển đổi mảng liên kết thành XML trong PHP
  • Chuyển đổi XML thành Mảng liên kết trong PHP
  • Chuyển đổi một mảng thành JSON trong PHP
  • Chuyển đổi JSON thành mảng hoặc đối tượng trong PHP
<?php
$mystring ="pasta, tuna, eggs";
$result_string = "'" . str_replace(",", "','", $mystring) . "'";
echo "Input String: ".$mystring;
echo “Output String: ".$result_string;
?>

Chuỗi đầu vào. mì ống, cá ngừ, trứng
Chuỗi đầu ra. 'mì ống', 'cá ngừ', 'trứng'

Bạn cũng có thể thích


  • Làm việc với php. cấu hình tập tin ini
  • Câu lệnh điều khiển trong PHP
  • Chuyển đổi mảng liên kết thành XML trong PHP
  • Chuyển đổi XML thành Mảng liên kết trong PHP
  • Sử dụng câu lệnh đã chuẩn bị với PHP & MySQL
  • Cách tải tệp lên bằng PHP
  • Chuyển đổi một mảng thành JSON trong PHP
  • Chuyển đổi JSON thành mảng hoặc đối tượng trong PHP
  • Thao tác mảng PHP. đẩy, bật, dịch chuyển, bỏ dịch chuyển
  • Loại bỏ các từ lặp lại khỏi chuỗi trong PHP
  • Chuyển đổi một mảng PHP thành một chuỗi truy vấn
  • Hơn 15 biểu thức chính quy dành cho nhà phát triển PHP
  • 10 hàm thư mục quan trọng nhất trong PHP
  • 10 hàm PHP ít được biết đến nhưng hữu ích
  • Tập lệnh PHP để tải xuống các tệp lớn một cách đáng tin cậy