fgetcsv(),在 csv 字符串中包含 html 实体

问题描述 投票:0回答:1

我解析 CSV 字符串并收到错误的列数,我认为这是因为像 %2C%20 这样的 html 实体。

我收到了错误的列数,我认为这是因为像 %2C%20 这样的 html 实体。

请帮助我正确解析带有 html 实体的 CSV 字符串

        $file = fopen('data://text/plain,' . $csvString, 'r+');
        $fileOffers = [];

        while (($data = fgetcsv($file) ) !== FALSE ) {
            $fileOffers[] = $data;
        }

但是在一行中我有这个网址 https://typhur.sjv.io/c/77612/1936847/18771?kw=%22valentines%20day%20gifts%2C%20valentines%20day%20gift%20for%20men%22&prodsku=AF03-DTC

php csv parsing html-entities html-encode
1个回答
0
投票

根据提供的代码,使用 PHP 解析包含 URL 的 CSV 字符串,这里是更新的代码:

$file = fopen('data://text/plain,' . urlencode($csvString), 'r+');
$fileOffers = [];
while (($data = fgetcsv($file) ) !== FALSE ) {
    $fileOffers[] = $data;
}

URL 解码后,“%2C”值变为逗号值,即“,”值。为了实现所需的功能,在提供的代码中,这行代码:

$file = fopen('data://text/plain,' . $csvString, 'r+');

替换为这行代码:

$file = fopen('data://text/plain,' . urlencode($csvString), 'r+');

可以检查一下是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.