首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如果$string发现中断否则添加新的不起作用

如果$string发现中断否则添加新的不起作用
EN

Stack Overflow用户
提问于 2018-06-05 04:21:53
回答 1查看 47关注 0票数 -3

我做错了什么吗?这不会产生任何影响

$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);
}

如何搜索字符串,如果未找到则添加新名称?

EN

回答 1

Stack Overflow用户

发布于 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)) {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50688210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档