我有两个文本块,包含公司名称。两者都包含数百家公司的名称,较新的名单包含更多公司。如何从两个列表中删除重复的公司名称,以便仅留下新名称? 示例文本块1:
Company Name One, Address line 1, line 2, phone, email
..
Random text
..
Company Name Two
Address, Phone,email
..
Random text
..
Company Name 3
Address, Phone,email
示例文本块二:
..Random Text..
M/s Company Name One Extra Random Text, Address line 1, line 2, phone,
..random text...
M/s Company Name Two
Address, Phone
...
公司名称,地址等类似不相同。第二个块在所有公司名称之前都有 M / s 字样。 我想在php中这样做,也许使用正则表达式。
我想把公司名称列为匹配,例如。在上面给出的示例中,我想输出公司名称:公司名称一,公司名称二对两个测试块都是通用的。
更新:感谢@Wrikken,我有两个字符串的文字。我可以使用 M / s 来爆炸第二个块,然后获取一个数组。然后,我如何检查此数组中的每个项目以匹配第一个文本块,这是一个长字符串?
虽然我已经手工完成了这项工作,但我仍然想知道如何比较两个文本块的相似性,从而获得赏金。
更新:@Joyce Babu代码的输出
..Random Text.. ..Random Text.. ..Random Text.. ..Random Text.. ..Random Text.. ..Random Text.. ..Random Text.. ..Random Text.. ..Random Text.. ..Random Text.. M/s Company Name One Extra Random Text, Address line 1, line 2, phone, M/s Company Name One Extra Random Text, Address line 1, line 2, phone, M/s Company Name One Extra Random Text, Address line 1, line 2, phone, M/s Company Name One Extra Random Text, Address line 1, line 2, phone, M/s Company Name One Extra Random Text, Address line 1, line 2, phone, M/s Company Name One Extra Random Text, Address line 1, line 2, phone, M/s Company Name One Extra Random Text, Address line 1, line 2, phone, M/s Company Name One Extra Random Text, Address line 1, line 2, phone, M/s Company Name One Extra Random Text, Address line 1, line 2, phone, M/s Company Name One Extra Random Text, Address line 1, line 2, phone, M/s Company Name One Extra Random Text, Address line 1, line 2, phone, ..random text... ..random text... ..random text... ..random text... ..random text... ..random text... ..random text... ..random text... ..random text... ..random text... M/s Company Name Two M/s Company Name Two M/s Company Name Two M/s Company Name Two M/s Company Name Two M/s Company Name Two M/s Company Name Two M/s Company Name Two M/s Company Name Two M/s Company Name Two Address, Phone Address, Phone Address, Phone Address, Phone Address, Phone Address, Phone Address, Phone Address, Phone Address, Phone Address, Phone ... ... ... ... ... ... ... ... ... ... ... ...
输出@nikic
array(2) { [0]=> string(17) "..Random Text.. " [4]=> string(16) "Address, Phone " }
@Joyce Babu第二篇文章的输出
andom Text..andom Text..andom Text..andom Text..andom Text..andom Text..andom Text..andom Text..andom Text..andom Text..Company Name One Extra Random Text, Address line 1, line 2, phone,Company Name One Extra Random Text, Address line 1, line 2, phone,Company Name One Extra Random Text, Address line 1, line 2, phone,Company Name One Extra Random Text, Address line 1, line 2, phone,Company Name One Extra Random Text, Address line 1, line 2, phone,Company Name One Extra Random Text, Address line 1, line 2, phone,Company Name One Extra Random Text, Address line 1, line 2, phone,Company Name One Extra Random Text, Address line 1, line 2, phone,Company Name One Extra Random Text, Address line 1, line 2, phone,Company Name One Extra Random Text, Address line 1, line 2, phone,Company Name One Extra Random Text, Address line 1, line 2, phone,andom text...andom text...andom text...andom text...andom text...andom text...andom text...andom text...andom text...andom text...Company Name TwoCompany Name TwoCompany Name TwoCompany Name TwoCompany Name TwoCompany Name TwoCompany Name TwoCompany Name TwoCompany Name TwoCompany Name Tworess, Phoneress, Phoneress, Phoneress, Phoneress, Phoneress, Phoneress, Phoneress, Phoneress, Phoneress, Phone
@Joyce Babu最终守则
<?php
set_time_limit(500);
$arOld = file('olddata.txt');
$arNew = file('newdata.txt');
$G=0;
$c=0;
foreach($arNew as $line){
if(substr($line, 0, 4) == 'M/s '){
$c++;
echo "<BR/>".$c.".)";
$line = trim(substr($line, 4));
foreach($arOld as $old){
similar_text($line, $old, $percentage);
if ($percentage > 80){
continue;
}
}
echo $line;
}else{
$G++;
}
}
echo "<br/>".$G . " DID NOT MATCH";
?>
来自Joyce Babu最终代码的输出
1.)Company Name One Extra Random Text, Address line 1, line 2, phone,
2.)Company Name Two
4 DID NOT MATCH
答案 0 :(得分:2)
创建两者的数组(可能使用file()
函数,具体取决于文本的格式,或者可能只是内容上的explode()
),并使用array_diff()
。
答案 1 :(得分:2)
试试这个
set_time_limit(500)
$arOld = file('olddata.txt');
$arNew = file('newdata.txt');
foreach($arNew as $line){
if(substr($line, 0, 3) === 'M/s '){
$line = trim(substr($line, 3));
foreach($arOld as $old){
similar_text($line, $old, $percentage);
if ($percentage > 80){
continue;
}
}
echo $line;
}
}
答案 2 :(得分:1)
如果您只需要将此列表进行一次比较,我建议将文档转换为txt,然后您就可以使用正则表达式进行比较。否则你需要使用第三方软件来访问文档中的信息......比如这里可能Reading/Writing a MS Word file in PHP
答案 3 :(得分:1)
$oldList = file('oldList.txt');
$newList = file('newList.txt');
$list = array_udiff($newList, $oldList, 'compare');
function compare($new, $old) {
similar_text($old, substr($new, 3), $percent);
return $percent >= 80 ? 0 : 1;
}
这是我的基本想法。要查找80%相似的所有文本,请从$newList
中删除它们。您应该调整百分比以满足您的需求。 M/s
已删除substr($new, 3)
。
答案 4 :(得分:1)
如果没有用于唯一标识记录的关键字段,我认为您必须使用similar_text或levenshtein之类的内容。
$arOld = file('olddata.txt');
$arNew = file('newdata.txt');
foreach($arNew as $line){
$line = trim(substr($line, 3));
foreach($arOld as $old){
similar_text($line, $old, $percentage);
if ($percentage < 60){
echo $line;
}
}
}