PHP - Group Strings By Similarity / Substring

时间:2017-08-03 08:55:36

标签: php string substring grouping similarity

PHP

嗨,我一直在努力解决这个问题,但是无法找到解决方案,并且想知道是否有人可以提供帮助。

我需要对类似字符串进行分组,例如:

输入

超薄铝合金HDMI导线,1米蓝色
超薄铝合金HDMI导线,2米蓝色
超薄铝合金HDMI导线,3米蓝色
带限容器的冷冻儿童耳机
XLR插头到插座引线,3米
XLR插头到插座引线,6米
带限音器的Monster High Kids耳机
带有体积限制器的TMNT儿童耳机
蝙蝠侠儿童耳机与音量限制器
1-Gang电缆入口刷墙板白色/白色刷子50 x 45mm
2-Gang电缆入口刷墙板白色/白色刷子50 x 100mm
1-Gang电缆入口刷墙板白色/黑色刷子50 x 45mm
2-Gang电缆入口刷墙板白色/黑色刷子50 x 100mm
超薄铝合金HDMI导线,5米蓝色
超薄铝合金HDMI导线,7.5米蓝色
6.35毫米(1/4英寸)单声道杰克至杰克吉他导线,5米橙色
XLR插头到插座引线,0.5米 XLR插头到插座引线,1米
XLR插头到插座引线,2米

输出(分组数组)

超薄铝合金HDMI导线,1米蓝色
超薄铝合金HDMI导线,2米蓝色
超薄铝合金HDMI导线,3米蓝色
超薄铝合金HDMI导线,5米蓝色
超薄铝合金HDMI线,7.5米蓝色

XLR插头到插座引线,0.5米 XLR插头到插座引线,1米
XLR插头到插座引线,2米
XLR插头到插座引线,3米
XLR插头到插座引线,6米

1-Gang电缆入口刷墙板白色/白色刷50 x 45mm
2-Gang电缆入口刷墙板白色/白色刷子50 x 100mm
1-Gang电缆入口刷墙板白色/黑色刷子50 x 45mm
2-Gang电缆入口刷墙板白色/黑色刷子50 x 100mm

带有限音器的冷冻儿童耳机
带限音器的Monster High Kids耳机
带有体积限制器的TMNT儿童耳机
带有音量限制器的蝙蝠侠儿童耳机

6.35毫米(1/4英寸)Mono Jack to Jack Guitar Lead,5米橙色

1 个答案:

答案 0 :(得分:4)

更新:我找到了更好的解决方案

我从来没有使用过php的similar_text()函数,但我想我还试一试......

$array = explode(PHP_EOL,'Slim Aluminium HDMI Lead, 1m Blue
Slim Aluminium HDMI Lead, 2m Blue
Slim Aluminium HDMI Lead, 3m Blue
Frozen Kids Headphones with Volume Limiter
XLR Plug to Socket Lead, 3m
XLR Plug to Socket Lead, 6m
Monster High Kids Headphones with Volume Limiter
TMNT Kids Headphones with Volume Limiter
Batman Kids Headphones with Volume Limiter
1-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 45mm
2-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 100mm
1-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 45mm
2-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 100mm
Slim Aluminium HDMI Lead, 5m Blue
Slim Aluminium HDMI Lead, 7.5m Blue
6.35mm (1/4") Mono Jack to Jack Guitar Lead, 5m Orange
XLR Plug to Socket Lead, 0.5m
XLR Plug to Socket Lead, 1m
XLR Plug to Socket Lead, 2m');

$output = array();
while( empty( $array ) === false )
{
  $currentWord = array_shift( $array );
  $currentGroup = array( $currentWord );
  foreach( $array as $index => $word )
  {
    if( similar_text( $word, $currentWord, $percentage ) and $percentage > 80 )
    {
      $currentGroup[] = $word;
      unset( $array[ $index ] );
    }
  }
  $output[] = $currentGroup;
}

print_r($output);
// Array
// (
//     [0] => Array
//         (
//             [0] => Slim Aluminium HDMI Lead, 1m Blue
//             [1] => Slim Aluminium HDMI Lead, 2m Blue
//             [2] => Slim Aluminium HDMI Lead, 3m Blue
//             [3] => Slim Aluminium HDMI Lead, 5m Blue
//             [4] => Slim Aluminium HDMI Lead, 7.5m Blue
//         )
// 
//     [1] => Array
//         (
//             [0] => Frozen Kids Headphones with Volume Limiter
//             [1] => Monster High Kids Headphones with Volume Limiter
//             [2] => TMNT Kids Headphones with Volume Limiter
//             [3] => Batman Kids Headphones with Volume Limiter
//         )
// 
//     [2] => Array
//         (
//             [0] => XLR Plug to Socket Lead, 3m
//             [1] => XLR Plug to Socket Lead, 6m
//             [2] => XLR Plug to Socket Lead, 0.5m
//             [3] => XLR Plug to Socket Lead, 1m
//             [4] => XLR Plug to Socket Lead, 2m
//         )
// 
//     [3] => Array
//         (
//             [0] => 1-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 45mm
//             [1] => 2-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 100mm
//             [2] => 1-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 45mm
//             [4] => 2-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 100mm
//         )
// 
//     [4] => Array
//         (
//             [0] => 6.35mm (1/4") Mono Jack to Jack Guitar Lead, 5m Orange
//         )
// 
// )

关联阵列编辑

$products = array(
  array('id'=>'A','name'=>'Slim Aluminium HDMI Lead, 1m Blue'),
  array('id'=>'B','name'=>'Slim Aluminium HDMI Lead, 2m Blue'),
  array('id'=>'C','name'=>'Slim Aluminium HDMI Lead, 3m Blue'),
  array('id'=>'D','name'=>'1-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 45mm'),
  array('id'=>'E','name'=>'2-Gang Cable Entry Brush Wall Plate White/White Brushes 50 x 100mm'),
  array('id'=>'F','name'=>'1-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 45mm'),
  array('id'=>'G','name'=>'2-Gang Cable Entry Brush Wall Plate White/Black Brushes 50 x 100mm'),
  array('id'=>'H','name'=>'Slim Aluminium HDMI Lead, 5m Blue'),
  array('id'=>'I','name'=>'Slim Aluminium HDMI Lead, 7.5m Blue')
);
$output = array();
while( empty( $products) === false )
{
  $currentProduct = array_shift( $products );
  $currentGroup = array( $currentProduct );
  foreach( $products as $index => $product )
  {
    if( similar_text( $product['name'], $currentProduct['name'], $percentage ) and $percentage > 80 )
    {
      $currentGroup[] = $product;
      unset( $products[ $index ] );
    }
  }
  $output[] = $currentGroup;
}
print_r($output);