我有一个包含数百万条记录的.txt文件。现在我有另外一个包含20条记录的文件,现在我想要从第一个文件中搜索第二个文件匹配记录。我使用下面的代码使用php查找记录:
$million_records = file('file/snp-db.txt');
$search_word = file('file/test.txt');
foreach($searchword as $word){
foreach($million_records as $single_record){
if(strpos($single_record, $word) !== false){
echo $single_record .'<br>';
}
}
}
但是此代码仅返回最后一个值,如下所示: test.txt文件只有4条记录
rs12564807
rs3131972
rs148828841
rs12124819
现在我想从snp-db.txt文件中找到这些记录,这些记录有以这种格式记录的数百万条记录:
rs12564807 1 734462 AA
rs3131972 1 752721 GG
rs148828841 1 760998 CC
rs12124819 1 776546 AG
rs115093905 1 787173 GG
rs11240777 1 798959 AG
rs7538305 1 824398 AC
rs4970383 1 838555 CC
rs4475691 1 846808 CC
rs7537756 1 854250 AA
rs13302982 1 861808 GG
rs55678698 1 864490 CC
i6019299 1 871267 CC
现在我只收到了rs12124819的结果。 你能帮我解决这个代码的错误吗?
答案 0 :(得分:1)
问题在于每个$word
和$single_record
都带有一个隐藏的新行,您需要在$single_record
$word
中测试million_records
之前删除它
首先迭代$million_records = file('file/snp-db.txt');
$search_word = file('file/test.txt');
foreach ($million_records as $single_record) {
$single_record = preg_replace("/\r|\n/", '', $single_record);
foreach ($search_word as $word) {
$word = preg_replace("/\r|\n/", '', $word);
if (strpos($single_record, $word) !== false) {
echo $single_record."\r\n";
}
}
}
文件(只有一次)会快得多
`$scope.data = params.filter() ? $filter('filter')($scope.users, $scope.src_product) : $scope.users;`