我有以下功能。在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文件中有这些数据。我的函数跳过第一个标题行,读取第二行,但将数组剪切到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~ 发布于 2015-07-07 05:45:52
您正在使用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);
}用于fgetcsv()的PHP引用
https://stackoverflow.com/questions/31260453
复制相似问题