比较PHP中的字符串,如果它们相似则从数组中删除其中一个字符串
| 
 假设我有一个这样的数组: 
  
  >马队 – 有鬼吗 新阵列将具有: >马队 – 有鬼吗 您将如何将每个字符串与PHP中列表中的每个其他字符串进行比较,如果它们相似,则将其删除. 我认为这些相似: >马鞭 – 葬礼 另一个例子: >马队 – 拉雷多 
 你有多种选择. 
  
  对于每个选项,您应该在执行比较之前按摩相册名称.您可以通过剥离标点符号,按字母顺序(在某些情况下)对专辑名称中的单词进行排序等来完成此操作. 在每种情况下,当您进行比较时,如果从阵列中删除其中一个相册名称,则您的比较是对订单敏感的,除非您对要删除的相册名称进行规则.因此,如果比较两个相册名称并发现“相似”,则始终删除较长的相册名称可能是有意义的. 主要比较选项是 >简单的子串比较.检查相册名称是否在另一个内.首先删除标点符号并对不区分大小写进行比较(请参阅下面的第二个代码段). 无论如何……这里有2个解决方案. 第一个使用 两个代码片段的工作方式是它们使用 请注意,我应该使用第三个参数作为array_walk中的引用,有人可以帮我这样做吗?目前的解决方案是全局变量: Live example(69%相似度阈值) function compare($value,$key)
{
    global $array; // Should use 3rd argument of compare instead
    $value = strtolower(preg_replace("/[^a-zA-Z0-9 ]/","",$value));
    $value = explode(" ",$value);
    sort($value);
    $value = implode($value);
    $value = preg_replace("/[s]/",$value); // Remove any leftover s
    foreach($array as $key2 => $value2)
    {
        if ($key != $key2)
        {
            // collapse,and lower case the string            
            $value2 = strtolower(preg_replace("/[^a-zA-Z0-9 ]/",$value2));
            $value2 = explode(" ",$value2);
            sort($value2);
            $value2 = implode($value2);            
            $value2 = preg_replace("/[s]/",$value2);
              // Set up the similarity
            similar_text($value,$value2,$sim);
            if ($sim > 69)
            {     // Remove the longer album name
                unset($array[ ((strlen($value) > strlen($value2))?$key:$key2) ]);
            }
        }
    }
}
array_walk($array,'compare');
$array = array_values($array);
print_r($array);以上的输出是: Array
(
    [0] => Band of Horses - Is There a Ghost
    [1] => Band Of Horses - No One's Gonna Love You
    [2] => Band of Horses - The Funeral
    [3] => Band of Horses - Laredo
    [4] => Band of Horses - "The Great Salt Lake" Sub Pop Records
    [5] => Band of Horses perform Marry Song at Tromso Wedding
    [6] => Band of Horses,On My Way Back Home
    [7] => Band of Horses - cigarettes wedding bands
    [8] => Band Of Horses - I Go To The Barn Because I Like The
    [9] => Our Swords - Band of Horses
    [10] => Band of Horses - Monsters
)请注意,Mary的歌曲的短版本丢失了…所以它肯定是对其他东西的误报,因为长版本仍然在列表中…..但它们正是你想要的专辑名称. 子串方法: Live Example function compare($value,$key)
{
      // I should be using &$array as a 3rd variable.
      // For some reason couldn't get that to work,so I do this instead.
    global $array;   
      // Take the current album name and remove all punctuation and white space
    $value = preg_replace("/[^a-zA-Z0-9]/",$value);        
      // Compare current album to all othes
    foreach($array as $key2 => $value2)
    {
        if ($key != $key2)
        {
              // collapse the album being compared to
            $value2 = preg_replace("/[^a-zA-Z0-9]/",$value2);
            $subject = $value2;
            $pattern = '/' . $value . '/i';
              // If there's a much remove the album being compared to
            if (preg_match($pattern,$subject))
            {
                unset($array[$key2]);
            }
        }
    }
}
array_walk($array,'compare');
$array = array_values($array);
echo "<pre>";
print_r($array);
echo "</pre>";对于您的示例字符串,上面的输出(它显示2您不希望显示): Array  
(  
    [0] => Band of Horses - Is There a Ghost  
    [1] => Band Of Horses - No One's Gonna Love You  
    [2] => Band of Horses - The Funeral  
    [3] => Band of Horses - Laredo  
    [4] => Band of Horses - "The Great Salt Lake" Sub Pop Records  
    [5] => Band of Horses perform Marry Song at Tromso Wedding      // <== Oops
    [6] => 'Laredo' by Band of Horses on Q TV                       // <== Oops  
    [7] => Band of Horses,On My Way Back Home  
    [8] => Band of Horses - cigarettes wedding bands  
    [9] => Band Of Horses - I Go To The Barn Because I Like The  
    [10] => Our Swords - Band of Horses  
    [11] => Band Of Horses - "Marry song"  
    [12] => Band of Horses - Monsters  
)(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
