我可以在php函数中避免在explode中使用逗号吗?
我有以下功能。在PI-Detail-ASXX.txt文件中,数据的分隔符为“~”。我正在使用以下函数分解符号,但它也会删除“”、“”我可以在php函数中避免在explode中使用逗号吗?,php,arrays,text-files,explode,Php,Arrays,Text Files,Explode,我有以下功能。在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 ) { // he
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++;
}
}
}
您正在使用
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);
}
它工作得很好。比爆炸更好的选择。
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);
}