nekooee Posted May 29, 2013 Share Posted May 29, 2013 (edited) himy 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 May 29, 2013 by nekooee Quote Link to comment https://forums.phpfreaks.com/topic/278542-my-code-for-save-mobile-number-doesnt-work-correctly/ Share on other sites More sharing options...
nekooee Posted May 29, 2013 Author Share Posted May 29, 2013 (edited) When the last line of the info.txt I'm Inter and Create a blank line , code work correctly. Why? Edited May 29, 2013 by nekooee Quote Link to comment https://forums.phpfreaks.com/topic/278542-my-code-for-save-mobile-number-doesnt-work-correctly/#findComment-1432935 Share on other sites More sharing options...
Barand Posted May 29, 2013 Share Posted May 29, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/278542-my-code-for-save-mobile-number-doesnt-work-correctly/#findComment-1432937 Share on other sites More sharing options...
nekooee Posted May 29, 2013 Author Share Posted May 29, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/278542-my-code-for-save-mobile-number-doesnt-work-correctly/#findComment-1432945 Share on other sites More sharing options...
Barand Posted May 30, 2013 Share Posted May 30, 2013 As I said, it worked fine for me, but then I was using my own test data files. Attach your data files and I see if it makes a difference. Quote Link to comment https://forums.phpfreaks.com/topic/278542-my-code-for-save-mobile-number-doesnt-work-correctly/#findComment-1433297 Share on other sites More sharing options...
DavidAM Posted May 30, 2013 Share Posted May 30, 2013 $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. Quote Link to comment https://forums.phpfreaks.com/topic/278542-my-code-for-save-mobile-number-doesnt-work-correctly/#findComment-1433302 Share on other sites More sharing options...
nekooee Posted May 30, 2013 Author Share Posted May 30, 2013 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: in Notpad++ Show All character is enable. Quote Link to comment https://forums.phpfreaks.com/topic/278542-my-code-for-save-mobile-number-doesnt-work-correctly/#findComment-1433308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.