PHP根据txt文件的行验证从url获取的值$\u

PHP根据txt文件的行验证从url获取的值$\u,php,Php,我有一个无法解决的问题,因为我自学成才,还在探索php世界 我有一个文本文件,看起来像这样: 951753159 456787541 123156488 748651651 我得到了一个带有变量的url http://example.com/mypage.php?variable=951753159 我想检查url变量是否与txt文件行之一匹配,以便执行代码 我已经试过了 $search = $_GET["variable"]; $file = "variables.txt"; if

我有一个无法解决的问题,因为我自学成才,还在探索php世界

我有一个文本文件,看起来像这样:

951753159
456787541
123156488
748651651
我得到了一个带有变量的url

http://example.com/mypage.php?variable=951753159
我想检查url变量是否与txt文件行之一匹配,以便执行代码

我已经试过了

 $search = $_GET["variable"];
 $file = "variables.txt";
 if (preg_match('/^' . $search . '$/m', file_get_contents($file))) { 
 THE CODE THAT I WANT TO EXECUTE
 }
但由于某种原因,它与文件的全部内容相匹配

非常感谢您的帮助


提前感谢:)

尝试使用
文件()中的数组。

从上的文档中,文件的全部内容将作为字符串读取。这就是它与整个文件匹配的原因

您要使用的命令是,它将文件读入每行的数组。

使用
file
将文件读入数组

然后
array\u翻转
数组,使其值现在成为键

它允许我设置($array[$key])您可以这样做

<?php
#$search = $_GET["variable"];
$search = '123156488';
$file_txt = "content.txt";
$file = file($file_txt);//convert the txt in array
foreach ($file as $key => $value) {
    if (trim($search) == trim($value)) {
        print "DO something! " . $value;
    }
}?>

问候。
尼尔森。

可能的副本可能会对你有所帮助(可能的转载):你刚刚救了我的命
<?php
#$search = $_GET["variable"];
$search = '123156488';
$file_txt = "content.txt";
$file = file($file_txt);//convert the txt in array
foreach ($file as $key => $value) {
    if (trim($search) == trim($value)) {
        print "DO something! " . $value;
    }
}?>