PHP检查文件是否包含字符串 [英] PHP check if file contains a string
本文介绍了PHP检查文件是否包含字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试查看文件是否包含发送到页面的字符串.我不确定这段代码出了什么问题:
I'm trying to see if a file contains a string that is sent to the page. I'm not sure what is wrong with this code:
?php
$valid = FALSE;
$id = $_GET['id'];
$file = './uuids.txt';
$handle = fopen($file, "r");
if ($handle) {
// Read file line-by-line
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, $id) === false)
$valid = TRUE;
}
}
fclose($handle);
if($valid) {
do stufff
}
推荐答案
简单得多:
<?php
if( strpos(file_get_contents("./uuids.txt"),$_GET['id']) !== false) {
// do stuff
}
?>
针对有关内存使用情况的评论:
In response to comments on memory usage:
<?php
if( exec('grep '.escapeshellarg($_GET['id']).' ./uuids.txt')) {
// do stuff
}
?>
这篇关于PHP检查文件是否包含字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文