如何在数组中搜索字符串的一部分?
我想搜索整个字符串或字符串的一部分是否是数组的一部分。在 PHP 中如何实现这一点?
另外,我如何在其中使用 metaphone ?
示例:
array1={'India','USA','China'};
array2={'India is in east','United States of America is USA','Made in China'}
如果我在 array2
中搜索 array1
,则:
“印度”应匹配“印度位于东部”,对于美国和印度也是如此。中国。
I want to search whether the complete string or a part of the string is a part of the array. How can this be achieved in PHP?
Also, how can I use metaphone in it as well?
Example:
array1={'India','USA','China'};
array2={'India is in east','United States of America is USA','Made in China'}
If I search for array1
in array2
, then:
'India' should match 'India is in east' and similarly for USA & China.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
结果:
使用 Metaphone 比较棘手。您必须确定什么构成匹配。一种方法是使用要比较的两个值的 Methaphone 结果之间的编辑距离。
更新:请参阅@Andrea 的 解决方案可实现更合理的每个单词变音位比较。
这是一个粗略的例子:
......但它需要工作。如果阈值太高,它会产生重复项。您可能更喜欢分两次进行比赛。一个用于查找简单匹配,如我的第一个代码示例中所示,然后另一个用于与 Metaphone 匹配(如果第一个没有返回匹配项)。
Result:
Using Metaphone is trickier. You will have to determine what constitutes a match. One way to do that is to use the Levenshtein distance between the Methaphone results for the two values being compared.
Update: See @Andrea's solution for a more sensible per-word Metaphone comparison.
Here's a rough example:
...but it needs work. It produces duplicates if the threshold is too high. You may prefer to run the match in two passes. One to find simple matches, as in my first code example, and then another to match with Metaphone if the first returns no matches.
变音位盒也可以遵循 Mike 针对严格盒提出的相同结构。
我认为不需要额外的相似性函数,因为变音位的目的应该是为我们提供一个发音相同的单词所共有的键。
上面的代码打印出:
The metaphone case could also follow the same structure proposed by Mike for the strict case.
I do not think that an additional similarity function is needed, because the purpose of the metaphone should be to give us a key that is common to words that sound the same.
The above code prints out: