简体   繁体   中英

Merging files with php

So, I have two files In the first file, there is text:hash

In the other file there is hash:pass

I wrote some code to match the hash and then print the text (So when ran, I'd get an output of text:pass) It works fine and it finds all the information, however sometimes it will skip certain ones and just not find anything. (If I go and do it by hand the data is there) So I'm not sure why it will find most of them but not all. Anyway I'm hoping someone can help, the code is below:

<?php
$emailhash = file('emailhash.txt');
$hashpass = file('hashpass.txt');
$list = '';

foreach($emailhash as $data) {
    $data = str_replace("\r\n",'', $data);
    $array_emailhash = explode(":", $data);
    $email = $array_emailhash[0];
    $hash = $array_emailhash[1];

    foreach($hashpass as $data2) {
        $data2 = str_replace("\r\n",'', $data2);
        $array_hashpass = explode(":", $data2);
        $hash2 = $array_hashpass[0];
        $pass = $array_hashpass[1];

        if($hash2 == $hash)
            $list .= $email.':'.$pass."\r\n";

    }
}

file_put_contents('emailpass.txt', $list);

You need to iterate your iterator, for example using multipleIterator , since you reads the entire file into an array, do like this.

$iterator = new MultipleIterator();
$iterator->attachIterator( new ArrayIterator( $emailhash ));
$iterator->attachIterator( new ArrayIterator( $hashpass ));

foreach( $iterator as $value ) {
    list($keys1, $keys2) = $iterator->key();
    list($value1, $value2) = $value;
}

Take a look at this -> http://br1.php.net/MultipleIterator

This algorithm is very inefficient. try to do this like that

$emailhash = file('emailhash.txt'); $hashpass = file('hashpass.txt'); $hashToEmail = array(); $list = '';

foreach($emailhash as $data) {
    $data = str_replace("\r\n",'', $data);
    $array_emailhash = explode(":", $data);
    $email = $array_emailhash[0];
    $hash = $array_emailhash[1];

    $hashToEmail[$hash] = $email;
}

foreach($hashpass as $data2) {
    $data2 = str_replace("\r\n",'', $data2);
    $array_hashpass = explode(":", $data2);
    $hash2 = $array_hashpass[0];
    $pass = $array_hashpass[1];
    if (array_key_exists($hash2, $hashToEmail)) {
        $list .= $hashToEmail[$hash2].':'.$pass."\r\n";
    } else {
        //what to do if you don't have that password hash
    }
}

Your design has Big O complexity of n^2 [for 1000 rows it will take 1 000 000 iterations] the one without nested foreach has a complexity of 2n. [it will take 2000 iterations]. Try to find a solution for that :)

Anyway in both places there is a issue, because you can have a hash collisions (for two different emails you can have the same hash, then for all emails you will print out same (probably wrong password).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM