我有这段代码,将csv文件的每一行存储到一个数组中:
$tname = myfile.csv
if ($type == 'text/csv'){
$csvData = file_get_contents($tname);
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $line){
$array[] = str_getcsv($line);
echo $line."<br>";
}
}
如何仅存储包含"John is great!"字符串的整行并丢弃其他行?
你应该使用 strpos(( 或 preg_match(( 函数。
if (strpos($line, 'John is great!') !== false) {
echo 'true';
}
或
if (preg_match('/John is great!/i',$line)) // i for case insensitive
echo 'true';
$tname = myfile.csv
if ($type == 'text/csv'){
$csvData = file_get_contents($tname);
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $line){
if (stripos($line, 'John is great!') !== false) {
$array[] = str_getcsv($line);
echo $line."<br>";
}
}
}
我假设您想要不区分大小写的匹配