如果存在值,则显示表行

时间:2011-11-14 17:45:33

标签: php tabular

我有一个用PHP抓取的文件。它有3行3列,具有这些值

John - 35000 - OL
Adam - 4000 - AF
John - 5000 - XS

我想要做的只是显示包含“John”这个词的行 我该怎么办?

3 个答案:

答案 0 :(得分:2)

foreach($rows as $row){
  if(strpos('John') !== false)
    echo $row;
}

答案 1 :(得分:0)

您可以使用filepreg_grep

// Put the file into an array
$file = file( $file_contents );

// Get only the lines that have John
$john = preg_grep( '~^John~', $file );

大约有一百万种方法可以做到这一点

答案 2 :(得分:0)

explode()行并检查第一个值:

$row_array = explode(" - ", $row);

if ($row_array[0] == "John") {
  // Row contains John
} else {
  // Row does not
}

由于您正在进行抓取,因此您可以轻松地解析各个值,因为它们位于数组中。