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

Edited by nekooee
Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

 

$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.

Link to comment
Share on other sites

I got the "trim", and my problem was solved.

If I do not use the trim Duplicate numbers in the last line of info.txt (@filename_S) will be different. See the photos:

 

sbqov2dr.jpg

 

in Notpad++ Show All character is enable.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.