Jump to content

my code for save mobile number doesn't work correctly


nekooee

Recommended Posts

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

//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

I was unable to re-create the error. It all seems to work fine for me. The only problem I can see is you are outputting an unnecessary linefeed at the end.

echo '<center dir="ltr">'.$Number.' new number saved</center>';
fwrite($file_D,"\n");                   // <-------- not required
fclose($file_S);
fclose($file_D);

thank you

I change my code:

//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>';
fclose($file_S);
fclose($file_D);

but work correctly only if  the last line of the info.txt I'm Inter and Create a blank line ! why? Otherwise, the last number is recorded twice.

Why?

 

$file_D=fopen("$filename_D", "a+")or die("Error: Can't open the file.");
 
while(!feof($file_D))
{
    $exists_numbers_D[]=fgets($file_D);
}

 

You open the "D" file with "a+", which opens it for Append and puts the pointer at the end of the file. I would think, then, that feof($file_D) will be true, the loop will not run, and the $exists_numbers_D will be empty.

 

In my opinion, instead of that loop, try using $exists_numbers_D = file($filename_D); before you open the file for appending. That one statement will do exactly what you are trying to do with the loop, so take that loop out.

 

You might also try using print_r or var_dump to see what is in that array to see if you actually read anything.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.