我正在使用此代码来检查输入的电子邮件在文本文件中是否存在
$handle = fopen("http://mywebsite/u.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line = preg_replace('/\s+/', ' ', $line);
if($line=$email){
echo "email existe";
}
}
fclose($handle);
} else {
// error opening the file.
}
在文本文件中存在电子邮件和不存在电子邮件的输出是相同的
电子邮件存在,电子邮件存在,存在电子邮件
文本文件包含作为测试电子邮件
如果我这样做==当$ line = $ email时没有输入条件
尝试使用
echo $line." ".$email;
输出
[email protected] test @ test.comtest @ test.com test @ test.comtest @ [email protected]
您的代码由于两个原因而失败。
1] if($line=$email)
是一种分配方法,将始终为TRUE。您正在寻找使用==
比较方法的方法。
2)您需要使用trim()
,因为这将处理文件中包含的换行符。这是另一位成员在Aynber处概述的内容。对他们表示敬意。
每:
“您可能需要同时修剪行和电子邮件,以确保没有空格/换行符– aynber”。
和
“放var_dump($ line);在preg_replace之后,它后面仍然有一个空格。”
我不知道我是否正确理解您,但是在这里您对问题的解决方法略有不同
// File from http
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$handle = curl_exec($ch);
curl_close($ch);
// or
$handle = file_get_contents("u.txt");
//or
$handle = "[email protected] \n [email protected]";
$find = "[email protected]";
if (strpos($handle, $find) > -1){
echo "Found" ;
} else{
echo "Not Found";
}