Я пытаюсь проверить, содержит ли файл строку, отправляемую на страницу. Я не уверен, что не так с этим кодом:
?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
}
Этот код более эффективен при поиске больших файлов.
$handle = fopen('path_to_your_file', 'r');
$valid = false; // init as false
while (($buffer = fgets($handle)) !== false) {
if (strpos($buffer, $id) !== false) {
$valid = TRUE;
break; // Once you find the string, you should break out the loop.
}
}
fclose($handle);
<?php
function getDirContents($dir, &$results = array()){
$files = scandir($dir);
foreach($files as $key => $value){
$path = realpath($dir.DIRECTORY_SEPARATOR.$value);
if(!is_dir($path)) {
$results[] = $path;
} else if($value != "." && $value != "..") {
getDirContents($path, $results);
$results[] = $path;
}
}
return $results;
}
$res = getDirContents('path');
$searchfor = 'search string';
foreach ($res as $file) {
if(is_dir($file)) {}else{
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){ ?>
<tr>
<td> <?php $string = implode("\n", $matches[0]); echo str_replace($searchfor,'<strong style="background-color:#ffff00">'.$searchfor.'</strong>',$string); ?> </td>
<td> <?php echo $file; ?> </td>
</tr>
<?php }else{
//echo "No matches found";
}
}
}
?>