比较PHP中的字符串,如果它们相似则从数组中删除其中一个字符串

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了比较PHP中的字符串,如果它们相似则从数组中删除其中一个字符串脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我有一个这样的数组:

>马队 – 有鬼吗
>马的乐队 – 没有人会喜欢

>马鞭 – 葬礼
>马的乐队 – 葬礼(歌词
在描述中)
>马队 – 拉雷多
>马的乐队 – 莱特多在莱特曼
10年5月20日
>马队 – “大盐
湖“子弹唱片
>马匹乐队 – “没有人会喜欢
您”
>马匹乐队演奏Marry Song
特罗姆瑟婚礼
>马的乐队 – 没有人会喜欢

> Q视上的Band of Horses的’Laredo’
>马的乐队,在我回家的路上
>马的乐队 – 香烟婚礼

>马匹乐队 – “香烟婚礼
带”
>马队 – 我去谷仓
因为我喜欢
>我们的剑 – 马的乐队
>马匹乐队 – “嫁给歌曲
>马匹乐队 – 怪兽
>马的乐队 – 没有人会喜欢

新阵列将具有:

>马队 – 有鬼吗
>马的乐队 – 没有人会喜欢

>马鞭 – 葬礼
>马队 – 拉雷多
>马队 – “大盐
湖“子弹唱片
>马的乐队,在我回家的路上
>马的乐队 – 香烟婚礼

>马队 – 我去谷仓
因为我喜欢
>我们的剑 – 马的乐队
>马匹乐队 – “嫁给歌曲”
>马匹乐队 – 怪兽

您将如何将每个字符串与PHP中列表中的每个其他字符串进行比较,如果它们相似,则将其删除.

我认为这些相似:

>马鞭 – 葬礼
>马鞭 – 葬礼(描述中的歌词)

一个例子:

>马队 – 拉雷多
>马的乐队 – 莱特多在莱特曼
10年5月20日

你有多种选择.

对于每个选项,您应该在执行比较之前按摩相册名称.您可以通过剥离标点符号,按字母顺序(在某些情况下)对专辑名称中的单词进行排序等来完成此操作.

在每种情况下,当您进行比较时,如果从阵列中删除其中一个相册名称,则您的比较是对订单敏感的,除非您对要删除的相册名称进行规则.因此,如果比较两个相册名称并发现“相似”,则始终删除较长的相册名称可能是有意义的.

主要比较选项是

>简单的子串比较.检查相册名称是否在另一个内.首先删除标点符号并对不区分大小写进行比较(请参阅下面的第二个代码段).
>使用levenshtein()检查相册名称相似性.此字符串比较比similar_text()更有效.你应该删除标点符号并按字母顺序排序.
>使用similar_text()检查专辑名称的相似性.我对这种方法运气最好.事实上,我选择了你想要的确切专辑名称(参见下面的第一个代码片段).
>您可以使用各种其他字符串比较功能,包括soundex()metaphone()

无论如何……这里有2个解决方案.

一个使用similar_text() …但它只是在所有标点符号被剥离并且单词按字母顺序排列并且小写之后才计算相似性……不利的一点是你必须使用阈值相似性…第二个在删除所有标点符号和空格后,使用简单的不区分大小写的子字符串测试.

两个代码片段的工作方式是它们使用array_walk()在数组中的每个专辑上运行compare()函数.然后在compare()函数中,我使用foreach()将当前专辑与所有其他专辑进行比较.有足够的空间来提高效率.

请注意,我应该使用第三个参数作为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  
)

脚本宝典总结

以上是脚本宝典为你收集整理的比较PHP中的字符串,如果它们相似则从数组中删除其中一个字符串全部内容,希望文章能够帮你解决比较PHP中的字符串,如果它们相似则从数组中删除其中一个字符串所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。