Php 多重比较和编辑文件
这是我的$file1结构,有数千个类似的组:Php 多重比较和编辑文件,php,html,file,preg-match,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
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
.....
这是我的Worker脚本,它生成并比较$file1和$file2
是什么让这个脚本:
比较$file1和$file2,并显示$file2中未定义的值
到目前为止,一切正常
我想稍微扩展一下我的脚本,所以我想用我的$file2结构替换{$value}。
这次,我不想检查该值,我想直接从$file1值替换它。斯帕达等。。。
我应该走哪条路。。。?请给我举一些例子……在$file1和$file2中是什么格式的?只是编出来的格式?它们只是TXT文件,只是让我知道它们是文本文件。我的意思是文本文件的格式与文本的外观相同。两个文件中都没有空格,仅使用[TAB]进行格式设置。您知道什么是JSON吗?
<?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>";
}
}
?>