查找使用PHP的CSV文件中是否存在值 [英] Find if a value exist in a CSV file with PHP

查看:346
本文介绍了查找使用PHP的CSV文件中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,检查我的.csv文件的行是否包含特定的名称,但它不工作。



我认为它有事情与 if 语句。

  $ file_handle = fopen csv,r); 

while(!feof($ file_handle)){

$ line_of_text = fgetcsv($ file_handle,1024);

if($ line_of_text [0] ='paul'){
echo'Found';
}
}
fclose($ file_handle);

我试图检查sources.csv文件,名为Paul。



由于技术原因,我不能使用像MySQL这样的数据库。

解决方案

由于您没有提供您的档案样本,因此我提交以下作为替代方案。



请注意,在当前代码中,您使用单个等号 = 分配而不是使用 == === 只是说为FYI



if($ line_of_text [0] ='paul')应读为 if($ line_of_text [0] =='paul ')



假设有以下 .csv 格式(即使没有逗号)并且区分大小写,请参阅脚注关于不区分大小写的搜索。

  Paul,Larry, Robert 






代码:

 <?php 
$ search =Paul
$ lines = file('sources.csv');
$ line_number = false;

while(list($ key,$ line)= each($ lines)and!$ line_number){

$ line_number =(strpos($ line,$ search) !== FALSE);

}

if($ line_number){

echo找到的结果。

}

else {
echo找不到与$ search相符的结果;
}






脚注:



对于不区分大小写的搜索,请使用 stripos()

  $ line_number =(stripos($ line,$ search)!== FALSE); 






...



为您提供找到的行号码:

  $ line_number =(strpos($ line,$ search)!== FALSE)? $ count:$ line_number; 

或(不区分大小写)

  $ line_number =(stripos($ line,$ search)!== FALSE)? $ count:$ line_number; 

然后只需 echo $ line_number; p>

This is my code to check if a row of my .csv file contains a specific name, but it does not work.

I think it has something to do with the if statement.

$file_handle = fopen("sources.csv", "r");

while (!feof($file_handle) ) {

    $line_of_text = fgetcsv($file_handle, 1024);

    if ($line_of_text[0] = 'paul') {
        echo 'Found';
    }
}
fclose($file_handle);

I am trying to check in sources.csv files, for the name 'Paul' .

I can't use a database like MySQL for technical reasons.

解决方案

Since you haven't provided a sample of your file, am submitting the following as an alternative.

Do note that in your present code, you are assigning using a single equal sign = instead of comparing using == or ===, just saying as an FYI.

if ($line_of_text[0] = 'paul') should read as if ($line_of_text[0] == 'paul')

Assuming the following .csv format (will work even without the commas) and is case-sensitive, consult Footnotes regarding case-insensitive search.

Paul, Larry, Robert


Code:

<?php
$search      = "Paul";
$lines       = file('sources.csv');
$line_number = false;

while (list($key, $line) = each($lines) and !$line_number) {

   $line_number = (strpos($line, $search) !== FALSE);

}

if($line_number){

   echo "Results found for " .$search;

}

else{
   echo "No results found for $search";
}


Footnotes:

For a case-insensitive search, use stripos()

$line_number = (stripos($line, $search) !== FALSE);


Row number found on...

To give you the row's number where it was found:

$line_number = (strpos($line, $search) !== FALSE) ? $count : $line_number;

or (case-insensitive)

$line_number = (stripos($line, $search) !== FALSE) ? $count : $line_number;

then just echo $line_number;

这篇关于查找使用PHP的CSV文件中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆