我做错了什么吗?这不会产生任何影响
$id = $_POST['id'];
$tudof = "\n #QTP ".$qtp." ID: ".$id;
echo "\n";
$fp = fopen('../../../ids.txt', 'a+');
$searchString = "id";
if(exec('grep '.escapeshellarg($searchString).' '.$fp)) {
break;
} else {
// Add the new name
fwrite($fp, $writef);
fclose($fp);
}
如何搜索字符串,如果未找到则添加新名称?
发布于 2018-06-05 11:39:16
我相信这会奏效的。
我使用file_get_contents将文件加载为一个字符串,并使用strpos查找"id“。
我还包含了一个preg_match版本,因为preg_match会将"id“与"lid”匹配,而strpos不会。
$id = $_POST['id'];
$tudof = "\n #QTP ".$qtp." ID: ".$id;
echo "\n";
$str = file_get_contents('../../../ids.txt');
$searchString = "id";
if(strpos($str, $searchString) !==false) {
// Found it! Break won't work.
// If you want to stop all php code use exit;
} else {
// Add the new name
// Not sure what you do here but use file_put_contents
file_put_contents('../../../ids.txt', $str);
}
// Preg_match
if(preg_match("/\b " . $str ."\b/", $searchString)) {
https://stackoverflow.com/questions/50688210
复制相似问题