Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

my code for save mobile number doesn't work correctly

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

my code for save mobile number doesn't work correctly

Postby nekooee » Wed May 29, 2013 1:10 am

hi
my code for save mobile number doesn't work correctly. please help me:

Code: Select all
//config
$filename_S="info.txt";//source file
$filename_D="output.txt";//destination file
 
//code...
$exists_numbers_S = array();
$file_S=fopen("$filename_S", "r")or die("Error: Can't open the file.");
$file_D=fopen("$filename_D", "a+")or die("Error: Can't open the file.");
 
while(!feof($file_D))
{
    $exists_numbers_D[]=fgets($file_D);
}
$Number=0;
while(!feof($file_S))
{
    $line=fgets($file_S);
    if (preg_match("/^09([0-9]{9,13})/", $line))
    {
            if ( !in_array($line, $exists_numbers_S) && !in_array($line, $exists_numbers_D)) {
                       fwrite($file_D,"$line");
                    $Number++;
                    $exists_numbers_S[] = $line;
            }
    }
}
echo '<center dir="ltr">'.$Number.' new number saved</center>';
fwrite($file_D,"\n");
fclose($file_S);
fclose($file_D);


The last number is stored in duplicate. Also after each refresh the page, last number is saved once again. the cod must not save repeat number. please help me
nekooee
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed May 29, 2013 12:22 am

Re: my code for save mobile number doesn't work correctly

Postby johnj » Wed May 29, 2013 1:23 am

Just before closing the files why are you writing to the file again?
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: my code for save mobile number doesn't work correctly

Postby nekooee » Wed May 29, 2013 3:56 am

Thank you. After adding a new number to source files (info.txt) , We require to re-operation.
But I've found the problem.
When the last line of the info.txt I'm Inter and Create a blank line , code work correctly. Why?
nekooee
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed May 29, 2013 12:22 am

Re: my code for save mobile number doesn't work correctly

Postby johnj » Wed May 29, 2013 5:12 am

because this condition if (preg_match("/^09([0-9]{9,13})/", $line)) is not satisfied
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.