我的言辞清单中有一些是言词,例如,
- palanca
- plato
- platopalanca
I need to remove "plato" and "palanca" and let only "platopalanca". Used array_unique to remove duplicates, but those composed words are tricky...
Should I sort the list by word length and compare one by one? A regular expression is the answer?
更新: 言词清单更大,有好有坏,而不仅仅是相关词。
更新2:我可以安全地将阵列化为扼杀。
更新3:我试图避免这样做,好像这样说。 必须有更有效的方式这样做。
Well, I think that a buble-sort like approach is the only possible one :-( I don t like it, but it s what i have... Any better approach?
function sortByLengthDesc($a,$b){
return strlen($a)-strlen($b);
}
usort($words, sortByLengthDesc );
$count = count($words);
for($i=0;$i<=$count;$i++) {
for($j=$i+1;$j<$count;$j++) {
if(strstr($words[$j], $words[$i]) ){
$delete[]=$i;
}
}
}
foreach($delete as $i) {
unset($words[$i]);
}
update 5: Sorry all. I m A moron. Jonathan Swift make me realize I was asking the wrong question. Given x words which START the same, I need to remove the shortests ones.
- "hot, dog, stand, hotdogstand" should become "dog, stand, hotdogstand"
- "car, pet, carpet" should become "pet, carpet"
- "palanca, plato, platopalanca" should become "palanca, platopalanca"
- "platoother, other" should be untouchedm they both start different