【问题标题】:eregi or preg from txt file in php [duplicate]php中txt文件中的eregi或preg [重复]
【发布时间】:2017-02-16 21:47:33
【问题描述】:

我有网站销售收费卡,我使用此代码向客户销售他们验证手机号码,手动(每次添加一行)最近我使用 SMS 服务和存储在网站文本文件中的号码,如何删除这些行,我从 .txt 文件中得到数字? (txt文件中逐行存储的数字)

if ($data[mobile] AND !eregi("^09512362982$", $data[mobile]))
if ($data[mobile] AND !eregi("^09527415120$", $data[mobile]))
if ($data[mobile] AND !eregi("^09532247462$", $data[mobile]))
if ($data[mobile] AND !eregi("^09535241919$", $data[mobile]))
if ($data[mobile] AND !eregi("^09535716327$", $data[mobile]))
$error .= "Mobile number not valid, please verify your number<br />";

【问题讨论】:

  • 您可以使用| 进行正则表达式更改。您可以使用file 从文件、file_get_contentsfread 中获取数据。 ^(09512362982|09527415120|etc.)$ 1 个正则表达式,而不是每个变体 1 个。似乎数据库更适合..
  • Lordie,您使用的是什么版本的 PHP? eregi() 在 PHP 5.3.0 中已弃用,在 PHP 7.0.0 中已删除。
  • 好点@Fred-ii-,OP 见stackoverflow.com/questions/6270004/…
  • 另外,您有什么理由不为此使用数据库?是时候跨入 21 世纪了。文本文件的工作量很大。
  • eregi 几乎只是在做!=

标签: php numbers eregi


【解决方案1】:

您可以使用file 将您的文本文件的内容读入一个数组,然后使用in_array 来查看是否有数字。

if (!in_array($_data['mobile'], file('your_text_file.txt', FILE_IGNORE_NEW_LINES))) {
    $error .= "Mobile number not valid, please verify your number<br />";
}

【讨论】:

  • 感谢回复,但是不行,错误500
  • @php-Lover 对不起,我在代码中有错字(缺少结束括号)。我刚刚更正了。
  • 感谢您的回复
  • 感谢您的回复,但无法读取我在此方法中使用的 .txt 文件,您能帮我解决问题 ***** if (!$data[mobile] AND !$data[mobile]) $error .= '你必须输入手机号码‌
    '; if ($data[email] AND filter_var($data[email], FILTER_VALIDATE_EMAIL)== false) $error .= '电子邮件地址不正确
    '; if (in_array(file('s1.txt', $_data['mobile'], FILE_IGNORE_NEW_LINES))) { // 号码在文件中 } else { // 不是 } $error .= "请验证您的号码, 通过发送 454 到 520501
    "; } if($error)
  • 在该注释的代码中,您在我的代码的 if/else 块之后附加了错误。您需要在 if 块中附加它。我更新了答案以表明我的意思。
猜你喜欢
  • 2013-02-09
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多