$txt_file = file('a.txt'); $line14 = $key; // 获取 $key 变量的值 $firstLine = '123'; $baidu = $firstLine;
$keys1 = [
'exceeded your current quota' => function() use ($line14, $txt_file) { // 在a.txt中找到对应的内容行并删除 foreach ($txt_file as $key => $line) { if (trim($line) === trim($line14)) { unset($txt_file[$key]); break; } } array_pop($txt_file); // 删除最后一行 file_put_contents('a.txt', implode("", $txt_file)); // 重新写入a.txt文件 //追加内容到文件的代码 file_put_contents("d.txt", $baidu . "\n", FILE_APPEND); exit; }, 'OpenAI account associated with this API key' => function() use ($line14, $txt_file) { // 在a.txt中找到对应的内容行并删除 foreach ($txt_file as $key => $line) { if (trim($line) === trim($line14)) { unset($txt_file[$key]); break; } } array_pop($txt_file); // 删除最后一行 file_put_contents('a.txt', implode("", $txt_file)); // 重新写入a.txt文件 //追加内容到文件的代码 file_put_contents("d.txt", $baidu . "\n", FILE_APPEND); exit; }, 'key is associated with a deactivated account' => function() use ($line14, $txt_file) { // 在a.txt中找到对应的内容行并删除 foreach ($txt_file as $key => $line) { if (trim($line) === trim($line14)) { unset($txt_file[$key]); break; } } array_pop($txt_file); // 删除最后一行 file_put_contents('a.txt', implode("", $txt_file)); // 重新写入a.txt文件 //追加内容到文件的代码 file_put_contents("d.txt", $baidu . "\n", FILE_APPEND); exit; } ]; foreach ($keys1 as $keyd1 => $action) { if (strpos($answer, $keyd1) !== false) { $action(); break; } } 上面的代码中追加内容到d.txt为什么没有生效内容:根据提供的代码,追加内容到d.txt文件的代码是正确的。可能的原因是代码中的逻辑没有执行到相应的条件分支,导致追加内容的代码没有被执行。

请确保以下几点:

  1. 变量$answer的值包含了数组$keys1中的某个键。
  2. 确保$a.txt文件中的内容行与$line14变量的值相匹配,以便删除该行。
  3. 确保d.txt文件可写入,即该文件的权限设置正确。

如果上述条件都符合,但仍然没有追加内容到d.txt文件,请提供更多关于$answer、$keys1、a.txt和d.txt文件的信息,以便更好地帮助您解决问题。

PHP代码示例:根据API错误信息删除a.txt文件中的对应行并追加内容到d.txt文件

原文地址: https://www.cveoy.top/t/topic/pj5d 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录