-2

I have website selling charge cards, I use this code for selling to customers they verify the mobile number, manually (add a line every time) recently I use SMS service and numbers stored in text file in the site, how can remove these lines, and I get the numbers from the .txt files? (the numbers stored line by line in txt file)

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 />";
Toto
  • 89,455
  • 62
  • 89
  • 125
php-Lover
  • 13
  • 5
  • You can use `|` for regex alterations. You can use `file` to get data from a file, `file_get_contents`, or `fread`. `^(09512362982|09527415120|etc.)$` 1 regex, not 1 per variation. Also seems like something a DB would be better for.. – chris85 Oct 07 '16 at 20:08
  • 1
    Lordie, what version of PHP are you using? `eregi()`'s deprecated in PHP 5.3.0, and REMOVED in PHP 7.0.0. – Funk Forty Niner Oct 07 '16 at 20:08
  • 1
    Good point @Fred-ii-, OP see http://stackoverflow.com/questions/6270004/how-can-i-convert-ereg-expressions-to-preg-in-php. – chris85 Oct 07 '16 at 20:10
  • Plus, any reason you're not using a database for this? It's time to step into the 21st century. Text files are a lot of work. – Funk Forty Niner Oct 07 '16 at 20:12
  • The `eregi` is pretty much just doing `!=` anyway. – Don't Panic Oct 07 '16 at 20:12

1 Answers1

0

You can use file to read the contents of your text file into an array, and then in_array to see if the number is there.

if (!in_array($_data['mobile'], file('your_text_file.txt', FILE_IGNORE_NEW_LINES))) {
    $error .= "Mobile number not valid, please verify your number<br />";
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Thank's for reply , But not work, Error 500 – php-Lover Oct 10 '16 at 18:50
  • @php-Lover Sorry, I had a typo in the code (missing closing paren). I just corrected it. – Don't Panic Oct 10 '16 at 19:49
  • Thank you for reply – php-Lover Oct 14 '16 at 18:46
  • thank you for reply but can't read the .txt file i use it on this method, can you help me where it the problem ***** if (!$data[mobile] AND !$data[mobile]) $error .= 'You Must enter a mobile number‌
    '; if ($data[email] AND filter_var($data[email], FILTER_VALIDATE_EMAIL)== false) $error .= 'Email address incorrect
    '; if (in_array(file('s1.txt', $_data['mobile'], FILE_IGNORE_NEW_LINES))) { // number is in file } else { // it isn't } $error .= "Please verify your number, by sending 454 to 520501
    "; } if($error)
    – php-Lover Oct 14 '16 at 18:54
  • In the code in that comment, you appended to the error after the if/else block from my code. You need to append to it in the if block. I updated the answer to show what I mean. – Don't Panic Oct 14 '16 at 19:00
  • thank you again i use the code, and not work, but i make some changes, and use this, the problem is , can't find the number fron txt file and match it if available or not. numbers in txt file saved line by line: 0123456789 if ( file('s1.txt', in_array($_data['mobile'], FILE_IGNORE_NEW_LINES))) { $error .= "Mobile number not valid, please verify your number
    ";
    – php-Lover Oct 19 '16 at 08:36
  • i use the code : `if (in_array($data[mobile], file('number.txt', FILE_IGNORE_NEW_LINES))) $error .= 'Mobile Number Not Verifed
    ';` But there is problem, NOW the numbers existing in TXT file get error and the numbers not existing pass it, On the contrary, I want. What i must to do ? i hope you can help me
    – php-Lover Feb 12 '17 at 15:05
  • Use `!in_array` instead. I updated the answer. – Don't Panic Feb 12 '17 at 15:08
  • Thank soooooooooooo MUCH it's WORK thank you for help – php-Lover Feb 12 '17 at 15:20