【发布时间】:2011-12-21 20:03:12
【问题描述】:
我正在尝试在 php 中编写一个脚本,该脚本在两段文本中找到相似的短语。我问过这个Question
1. the cat is on the roof
2. a man is on the stage
A1 = [the, cat, is, on, the, roof]
A2 = [a, man, is, on, the, stage]
[the]: no match
[cat]: no match
[is]: match
[is, on]: match
[is, on, the]: match
[is, on, the, roof]: no match
[on]: match
[on, the]: match
[on, the, roof]: no match
[the]: match
[the, roof]: no match
[roof]: no match
-end-
我的代码如下。
<?php
echo match(explode(' ',$text1), explode(' ',$text2));
function match($old, $new){
$arr;
foreach($old as $key=>$oldword) {
foreach($new as $key2=>$newword) {
if($old[$key] == $new[$key2]) {
array_push($arr,$old[$key]);
echo '<span style="color:red;">'.$old[$key].' </span>';
}
else {
echo $old[$key].' ';
}
}
}
}
我得到以下输出
为什么单词会重复?
【问题讨论】: