我想在php中读取CSV文件,比如
203.88.0.0,203.88.31.255
84.64.0.0,84.71.255.255
123.63.128.0,123.63.255.255
112.79.0.0,112.79.255.255
1.38.0.0,1.39.255.255
114.31.128.0,114.31.191.255
并在变量中获取输出,如:
MAX_checkClient_Ip('10.52.81.60|10.52.81.61', '=^')
or MAX_checkClient_Ip('10.66.40.136|10.66.40.137', '=^')
or MAX_checkClient_Ip('10.99.53.15|10.99.53.21', '=^')
or MAX_checkClient_Ip('10.99.53.143|10.99.53.149', '=^')
or MAX_checkClient_Ip('31.173.64.0|31.173.71.255', '=^')
or MAX_checkClient_Ip('31.173.192.0|31.173.193.255', '=^')
or MAX_checkClient_Ip('31.173.240.0|31.173.243.255', '=^')
or MAX_checkClient_Ip('37.28.160.0|37.28.191.255', '=^')
or MAX_checkClient_Ip('37.29.0.0|37.29.15.255', '=^')
注意,第一行没有"或"
public function create_channel()
{
$file_handle = fopen("IP_SCRIPT.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
//print "or MAX_checkClient_Ip('".$line_of_text[0] ."|". $line_of_text[1]."', '=^')'r'n";
$str[]="or MAX_checkClient_Ip('".$line_of_text[0] ."|". $line_of_text[1]."', '=^')'r'n";
}
for($i=0;$i<count($str);$i++)
{
echo $str[$i];
}
fclose($file_handle);
}
但是我得到的输出是
or MAX_checkClient_Ip('203.88.0.0|203.88.31.255', '=^')
or MAX_checkClient_Ip('84.64.0.0|84.71.255.255', '=^')
or MAX_checkClient_Ip('123.63.128.0|123.63.255.255', '=^')
or MAX_checkClient_Ip('112.79.0.0|112.79.255.255', '=^')
or MAX_checkClient_Ip('1.38.0.0|1.39.255.255', '=^')
or MAX_checkClient_Ip('114.31.128.0|114.31.191.255', '=^')
or MAX_checkClient_Ip('|', '=^')
我不希望"or"出现在第一行,最后一行也不需要
稍微调整一下代码,将"or"的处理从读取循环移到打印循环中:
public function create_channel()
{
$file_handle = fopen("IP_SCRIPT.csv", "r");
while (!feof($file_handle))
{
$line_of_text = fgetcsv($file_handle, 1024);
$str[]="MAX_checkClient_Ip('".$line_of_text[0] ."|". $line_of_text[1]."', '=^')";
}
fclose($file_handle);
for($i=0; $i<count($str); $i++)
{
if ($i > 0) {
print "or ";
}
print $str[$i] . "'n";
}
}
@Aziz Saleh的建议很好。整个for
循环可以替换为:
print implode("'nor ", $str) . "'n";