如果之前的字符串与当前字符串相似,则不回显字符串
如果之前的字符串与当前的字符串相似,我不想回显字符串。假设我们的字符串是,
$strings = array("software","software","game","antivirus");
我的差异函数,
function ($val1,$val2) {
similar_text($val1,$val2,$percent);
if ($percent>83) {
// should not echo. But don't know how to do.
}
}
但我不知道该怎么做。我想应该是每个都使用。
I don't want to echo string if before string is similar to current string. Let's say our strings are,
$strings = array("software","software","game","antivirus");
My difference function,
function ($val1,$val2) {
similar_text($val1,$val2,$percent);
if ($percent>83) {
// should not echo. But don't know how to do.
}
}
But I don't know how can I do it. I guess it should be with using for each.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的事情:
如果您不明白其中的某些部分,请发表评论,我会澄清。
编辑:
我将
$lastString = $string;
移至条件内。考虑以下字符串列表:
$strings = array("software","sofware","software","ofwart","fwart","wart","warts");
保留
$lastString 赋值只会打印
software
,即使很多单词与software
非常不同,它们与前一个单词没有太大不同。将其移入内部实际上会给出输出:
Try something like this:
If you don't understand some part of it, leave a comment and I will clarify.
Edit:
I moved the
$lastString = $string;
inside the condition.Consider the following list of strings:
$strings = array("software","sofware","sofwart","ofwart","fwart","wart","warts");
Leaving the
$lastString
assignment outside of the loop would only printsoftware
even though lots of the words are very very differentsoftware
they are not so different from the previous word.Moving it inside actually gives the output :
但我认为最好这样做(应该更快):
顺便说一句,我完全不明白 $percent 的含义。
But I think it's better to do it with for like this (it should be faster):
Btw I totally did't get what the $percent means..
使用 array_filter() 的方法(假设 >= 5.3):
产量:
希望这会有所帮助。事实上,直到现在我才知道
similar_text()
。相当有趣的功能。谢谢 :)An approach using
array_filter()
(assumes >= 5.3):Yields:
Hope this helps. Actually, I never knew about
similar_text()
until now. Pretty interesting function. Thanks :)