我想搜索完整的字符串或字符串的一部分是否是数组的一部分。如何在PHP中实现这一目标?
另外,我如何在其中使用metaphone?
示例:
array1={'India','USA','China'};
array2={'India is in east','United States of America is USA','Made in China'}
如果我在array1
中搜索array2
,则:
'印度'应与“印度在东方”相匹配,同样适用于美国和美国。中国。
答案 0 :(得分:4)
$array1 = array('India','USA','China');
$array2 = array('India is in east','United States of America is USA','Made in China');
$found = array();
foreach ($array1 as $key => $value) {
// Thanks to @Andrea for this suggestion:
$found[$value] = preg_grep("/$value/", $array2);
// Alternative:
//$found = $found + preg_grep("/$value/", $array2);
}
print_r($found);
结果:
Array
(
[0] => India is in east
[1] => United States of America is USA
[2] => Made in China
)
使用Metaphone比较棘手。您必须确定匹配的内容。一种方法是使用Methaphone结果之间的Levenshtein距离来比较两个值。
更新:请参阅@Andrea's解决方案,了解更为明智的单词Metaphone比较。
这是一个粗略的例子:
$meta1 = array_map(
create_function( '$v', 'return array(metaphone($v) => $v);' ),
$array1
);
$meta2 = array_map(
create_function( '$v', 'return array(metaphone($v) => $v);' ),
$array2
);
$threshold = 3;
foreach ($meta2 as $key2 => $value2) {
$k2 = key($value2);
$v2 = $value2[$k2];
foreach ($meta1 as $key1 => $value1) {
$k1 = key($value1);
$v1 = $value1[$k1];
$lev = levenshtein($k2, $k1);
if( strpos($v2, $v1) !== false || levenshtein($k2, $k1) <= $threshold ) {
array_push( $found, $v2 );
}
}
}
......但它需要工作。如果阈值太高,它会产生重复。您可能更喜欢两次通过比赛。一个用于查找简单匹配,如在我的第一个代码示例中,然后另一个用于匹配Metaphone,如果第一个没有匹配。
答案 1 :(得分:1)
metaphone案例也可能遵循Mike针对严格案例提出的相同结构。
我认为不需要额外的相似性功能,因为metaphone的目的应该是给我们一个听起来相同的词的常用键。
$array1 = array('India','USA','China');
$array2 = array(
'Indiuh is in east',
'United States of America is USA',
'Gandhi was born in India',
'Made in China'
);
$found = array();
foreach ($array1 as $key => $value) {
$found[$value] = preg_grep('/\b'.$value.'\b/i', $array2);
}
var_export($found);
echo "\n\n";
function meta( $sentence )
{
return implode(' ', array_map('metaphone', explode(' ', $sentence)));
}
$array2meta = array_map('meta', $array2);
foreach ($array1 as $key => $value) {
$valuemeta = meta($value);
$foundmeta[$value] = preg_grep('/\b'.$valuemeta.'\b/', $array2meta);
$foundmeta[$value] = array_intersect_key($array2, $foundmeta[$value]);
}
var_export($foundmeta);
上面的代码打印出来:
array (
'India' =>
array (
2 => 'Gandhi was born in India',
),
'USA' =>
array (
1 => 'United States of America is USA',
),
'China' =>
array (
3 => 'Made in China',
),
)
array (
'India' =>
array (
0 => 'Indiuh is in east',
2 => 'Gandhi was born in India',
),
'USA' =>
array (
1 => 'United States of America is USA',
),
'China' =>
array (
3 => 'Made in China',
),
)
答案 2 :(得分:0)
$a1 = array('India','USA','China');
$a2 = array('India is in east','United States of America is USA','Made in China');
foreach ( $a2 as $a )
{
foreach( $a1 as $b )
{
if ( strpos( $a, $b ) > -1 )
{
echo $a . " contains " . $b . "\n";
}
}
}