-1

I'm new in PHP language , I have 2 csv files that contains:first 2 columns the first with some countries prefixes and the second with name of correspondent country. The second has 2 column the first has phone numbers and the second correspondent country. I need to make a compare of this 2 files if I found matching of prefixes, I need that this number to be deleted , and the rest of phone number to be added to another file. For example:

   First File x.csv         Second file y.csv


prefix  country          country     phone number
4474    UK               UK          44793245683...  
347466  Spain            Spain       34746689211345
3278    Belgium          Belgium     324568845212.....
                         Switzerland 4189544562131...
                         UK          4474321546588464...
                         Italy       39324566546548345
                         UK          4478564684151...   

So I want in another fil z.csv this output:

country     phone number
UK          44793245683...

Belgium     324568845212.....
Switzerland 4189544562131...
Italy       39324566546548345
UK          4478564684151... 

This means that in this numbers has found match of prefixes:

Country     Phone NUmber      Match Prefix
Spain       34746689211345    347466
UK          4474321546588464  4474 

Nick
  • 138,499
  • 22
  • 57
  • 95
Ervis Hysi
  • 55
  • 1
  • 9
  • 2
    you need to learn how to open csv files first, then start your way up into reading lines, then you can compare – Kevin May 14 '18 at 09:32
  • http://php.net/manual/en/function.fgetcsv.php – k0pernikus May 14 '18 at 09:34
  • 1
    This question is too broad and almost like asking "write me the code". Please read the documentation and if that fails, ask a more specific question for a specific problem with a code example instead of a requirement. – k0pernikus May 14 '18 at 09:35
  • This can be made with one [awk](https://stackoverflow.com/questions/17001849/awk-partly-string-match-if-column-word-partly-matches) command through [shell_exec](http://php.net/manual/en/function.shell-exec.php). – camelsWriteInCamelCase May 14 '18 at 09:36
  • 1
    Please read this "[How do I ask a good question?](https://stackoverflow.com/help/how-to-ask)" – camelsWriteInCamelCase May 14 '18 at 09:36

2 Answers2

0

You can use fgetcsv to read the csv filesfputcsv to write a csv file

for example :

$x = fgetcsv('x.csv');
$y = fgetcsv('y.csv');
$z = [];
foreach $x as $row
  if $row[1] $y then add $row in $z
fputcsv('z.csv', $z);
0

Here is an answer with example files and usage:

File one:

root@dib:~# cat 1.csv
prefix  country
4474    UK
347466  Spain
3278    Belgium

File two:

root@dib:~# cat 2.csv
country     phone number
UK          44793245683...
Spain       34746689211345
Belgium     324568845212.....
Switzerland 4189544562131...
UK          4474321546588464...
Italy       39324566546548345
UK          4478564684151...

Php script:

root@dib:~# cat find_numbers.php
<?php
$file1 = $argv[1];
$file2 = $argv[2];

// Get an array of each line of the first file.
$prefix_contry = explode("\n", file_get_contents($file1));

print "Country\t Phone NUmber\t Match Prefix\n";

foreach($prefix_contry as $prefix_line) {
    // Extract numbers from begging of each line.
    if(preg_match('/^([0-9]+)/', $prefix_line, $prefix_matches)) {

        if($file = fopen($file2, "r")) {

            // Loop through second file line by line...
            while(($line = fgets($file)) !== false) {
                $line = rtrim($line);

                // Pull out number from second file.
                if(preg_match('/([0-9]+)/', $line, $matches)) {

                    // Check to see if number begins with prefix.
                    if(preg_match("/^{$prefix_matches[1]}/", $matches[1])) {
                        print("$line\t{$prefix_matches[1]}\n");
                    }
                }
            }
            fclose($file);
        } else {   print "Couldn't open file.";   }
    }
}

?>

Test run:

root@dib:~# php find_numbers.php 1.csv 2.csv
Country  Phone NUmber    Match Prefix
UK          4474321546588464... 4474
Spain       34746689211345      347466
Tim
  • 2,139
  • 13
  • 18
  • // Check to see if number begins with prefix
      if(preg_match("/^{$prefix_matches[1]}/", $matches[1])) {
                            print("$line\t{$prefix_matches[1]}");
                        }
    From these condition I need the "else" part, but when I print out , Im faced with too much combination, even the combination with matched prefix
    – Ervis Hysi May 14 '18 at 13:59
  • @Ervis, could you reword that statement? What do you mean you need the else part? – Tim May 14 '18 at 23:12
  • when you make control "// Check to see if number begins with prefix." in this I need to see all results that don't have any match with prefixes. For example : 44793245683... this number has no match even this, but when I use large amount of prefixes and numbers Im faced with large of combination of phone numbers that repeat . – Ervis Hysi May 15 '18 at 07:26
  • You should reword the initial question then because your example output doesn't match what you are now asking for. Additionally, if you want things that don't match you could just print only the matches to a file, then use `grep -v -f ` which will remove any of the numbers that match the output file, thus giving you the ones that "don't have a match" – Tim May 17 '18 at 15:53
  • Additionally, you can do all this with shell commands much easier than with PHP. – Tim May 17 '18 at 15:56