如何获取JSON字符串差异百分比



是否有任何逻辑/算法可以比较具有差异百分比的两个JSON字符串。我正在尝试使用它来验证我存储为JSON字符串的签名。

字符串1:

[
  {"lx":5,"ly":34,"mx":5,"my":33},
  {"lx":5,"ly":34,"mx":5,"my":34},
  {"lx":6,"ly":32,"mx":5,"my":34},
  {"lx":7,"ly":31,"mx":6,"my":32},
  {"lx":8,"ly":31,"mx":7,"my":31},
  {"lx":9,"ly":30,"mx":8,"my":31},
  {"lx":10,"ly":29,"mx":9,"my":30},
  {"lx":11,"ly":29,"mx":10,"my":29},
  {"lx":11,"ly":28,"mx":11,"my":29}
]

字符串2:

[
  {"lx":5,"ly":33,"mx":5,"my":32},
  {"lx":5,"ly":33,"mx":5,"my":33},
  {"lx":6,"ly":32,"mx":5,"my":33},
  {"lx":7,"ly":32,"mx":6,"my":32},
  {"lx":8,"ly":32,"mx":7,"my":32},
  {"lx":9,"ly":32,"mx":8,"my":32},
  {"lx":10,"ly":31,"mx":9,"my":32},
  {"lx":11,"ly":30,"mx":10,"my":31},
  {"lx":12,"ly":30,"mx":11,"my":30},
  {"lx":13,"ly":29,"mx":12,"my":30}
]

如有任何建议,我们将不胜感激。

我没有得到你想要做的,但如果你只是想检查字符串的相似性,你可以使用similar_text.php命令

样品:

$str1 = '{"lx":5,"ly":34,"mx":5,"my":33},
  {"lx":5,"ly":34,"mx":5,"my":34},
  {"lx":6,"ly":32,"mx":5,"my":34},
  {"lx":7,"ly":31,"mx":6,"my":32},
  {"lx":8,"ly":31,"mx":7,"my":31},
  {"lx":9,"ly":30,"mx":8,"my":31},
  {"lx":10,"ly":29,"mx":9,"my":30},
  {"lx":11,"ly":29,"mx":10,"my":29},
  {"lx":11,"ly":28,"mx":11,"my":29}';
  $str2 = '{"lx":5,"ly":33,"mx":5,"my":32},
  {"lx":5,"ly":33,"mx":5,"my":33},
  {"lx":6,"ly":32,"mx":5,"my":33},
  {"lx":7,"ly":32,"mx":6,"my":32},
  {"lx":8,"ly":32,"mx":7,"my":32},
  {"lx":9,"ly":32,"mx":8,"my":32},
  {"lx":10,"ly":31,"mx":9,"my":32},
  {"lx":11,"ly":30,"mx":10,"my":31},
  {"lx":12,"ly":30,"mx":11,"my":30},
  {"lx":13,"ly":29,"mx":12,"my":30}';
  $percent = 0;
  echo similar_text($str1,$str2,$percent).' equal chars'.PHP_EOL;
  echo $percent.'% similarity';

给你:

269 equal chars
78.425655976676% similarity

字符串距离可以用Levenstein函数计算

遗憾的是,它被限制为不超过255个字符的字符串。

相关内容

  • 没有找到相关文章

最新更新