我可以在 php 函数中避免在 explode 中使用逗号吗?

can i avoid comma in explode in my php function?

我有以下功能。在 PI-Detail-ASXX.txt 文件中,数据的分隔符为“~”。我正在使用以下函数分解符号,但它也删除了 ","

function checkFeatures($productID,$count)
{
$fd = fopen('PI-Detail-ASXX.txt', 'r');
$fline = 0;

while ( ( $frow = fgetcsv($fd) ) !== false ) {
    if ($fline <=0 ) {
        // headings, so continue/ignore this iteration:
        $fline++;
        continue;
        }
    //for lines other than headers
   if($fline >0){
   $contents = explode("~", $frow[0]);
   print_r($contents);
   $fline++;
   }
 }
}

例如,如果您在 txt 文件中有此数据。我的函数跳过第一个 header 行,读取第二行但将数组剪切到 deploy, 并且只打印 3 个数组元素,因为我相信逗号。第三行正确打印了 5 个数组元素。有谁知道如何不让这种情况发生。

IMSKU~AttributeID~Value~Unit~StoredValue~StoredUnit  
1000001~7332~McAfee Host Intrusion Prevention for Desktops safeguards your business against complex security threats that may otherwise be unintentionally introduced or allowed by desktops and laptops. Host Intrusion Prevention for Desktops is easy to deploy, configure, and manage.~~~  
1000001~7343~May 2013~~~  
1000001~7344~McAfee~~0.00~  

您正在阅读带有 fgetcsv() 的文件,该文件默认以逗号分隔。此后,您将在 ~ 上爆发。您可以向 fgetcsv() 添加一个额外的参数,它会在 ~ 上直接分解为一个数组,之后不需要分解字符串。

这应该会给你思路,但我还没有测试过。

function checkFeatures($productID,$count)
{
    $fd = fopen('PI-Detail-ASXX.txt', 'r');
    $fheader = fgets($fd); // read and discard header first

    while ( ( $frow = fgetcsv($fd,0,'~') ) !== false ) {
        print_r($frow);
    }
    fclose($fd);
}

PHP Reference for fgetcsv()