多个比较和编辑文件

时间:2015-04-08 19:43:04

标签: php html file preg-match

这是我的$file1结构(有数千个类似的组):

Group   id_7653
{
    type    register
    sub_name    155
    1   3123    1   12  
    2   3124    1   8   
    3   3125    1   4   
    4   3126    1   12  
    5   3127    1   8   
    6   3128    1   4   
    .....
}

Group   id_8731
{
    type    register
    sub_name    155
    1   4331    1   12  
    2   4332    1   8   
    3   4333    1   4   
    4   4334    1   12  
    5   4335    1   8   
    6   4336    1   4   
    .....
}

这是我的$file2结构(同样,有数千个定义的值)

.....
3123    Spada+1
3124    Spada+2
3125    Spada+3
3126    Spada+4
3127    Spada+5
3128    Spada+6
3129    Spada+7
3130    Spada+8
.....

这是我的工作人员脚本,比较$file1$file2

<?php

//read the first file in as a string
$file1 = file_get_contents("dataparser\names1.txt");
//read the second file in as an array
$file2 = file("dataparser\names2.txt");

//index from file2 that we are going to build
$file2Index = array();

foreach($file2 as $line){
    //split the line
    $line = explode("\t", $line, 2);
    //validate the line, should be only 2 values after explode and first should be a number
    if(count($line) == 2 && is_numeric($line[0])){
        //add to index
        $file2Index[$line[0]] = $line[1];
    }
}

//now get all the values from file1 that we want (second column)
preg_match_all('/^\s*\d+\s*(\d+)\s*\d+\s*\d+\s*$/m', $file1, $matches);

$file1Values = array_unique($matches[1]);

//loop over the matches from column 2
foreach($file1Values as $value){
    //check if the key doesn't exist
    if(!isset($file2Index[$value])){
        //echo error message
        echo "Value {$value} does not exist in file2<br>";
    }
}

?>

该脚本是什么:

比较$file1$file2并告诉我$file2

中未定义哪些值

到目前为止,一切正常。

我想稍微扩展一下我的脚本,所以我想用$file2结构替换{$ value}。 这次,我不想检查该值,我想直接从$file1值替换它。 (斯巴达等......) 我应该遵循哪条路径?我可以得到一些例子......

0 个答案:

没有答案