如果之前的字符串与当前字符串相似,则不回显字符串

发布于 2025-01-03 04:49:40 字数 335 浏览 1 评论 0原文

如果之前的字符串与当前的字符串相似,我不想回显字符串。假设我们的字符串是,

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隐诗 2025-01-10 04:49:40

尝试这样的事情:

$strings = array("software","software","game","antivirus");

$lastString = '';

foreach ($strings as $string) {
    similar_text($lastString, $string, $percent);
    if ($percent < 83) {
        echo "$string<br />";
        $lastString = $string;
    }
}

如果您不明白其中的某些部分,请发表评论,我会澄清。

编辑:
我将 $lastString = $string; 移至条件内。

考虑以下字符串列表:
$strings = array("software","sofware","software","ofwart","fwart","wart","warts");

保留 $lastString 赋值只会打印 software,即使很多单词与 software 非常不同,它们与前一个单词没有太大不同。

将其移入内部实际上会给出输出:

软件
软件

Try something like this:

$strings = array("software","software","game","antivirus");

$lastString = '';

foreach ($strings as $string) {
    similar_text($lastString, $string, $percent);
    if ($percent < 83) {
        echo "$string<br />";
        $lastString = $string;
    }
}

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 print software even though lots of the words are very very different software they are not so different from the previous word.

Moving it inside actually gives the output :

software
sofwart
wart

高跟鞋的旋律 2025-01-10 04:49:40
$strings = array("software","software","game","antivirus");
$previous = '';
foreach ($strings as $string) {
  if ($string===$previous) {
    continue;
  } else {
    echo $string;
    $previous = $string;
  }
}

但我认为最好这样做(应该更快):

$strings = array("software","software","game","antivirus");
$num = count($strings);
for ($i=0;$i<$num;$i++) {
  if ($strings[$i]===$strings[$i-1] && $i!==0) {
    continue;
  } else {
    echo $strings[$i];
  }
}

顺便说一句,我完全不明白 $percent 的含义。

$strings = array("software","software","game","antivirus");
$previous = '';
foreach ($strings as $string) {
  if ($string===$previous) {
    continue;
  } else {
    echo $string;
    $previous = $string;
  }
}

But I think it's better to do it with for like this (it should be faster):

$strings = array("software","software","game","antivirus");
$num = count($strings);
for ($i=0;$i<$num;$i++) {
  if ($strings[$i]===$strings[$i-1] && $i!==0) {
    continue;
  } else {
    echo $strings[$i];
  }
}

Btw I totally did't get what the $percent means..

[旋木] 2025-01-10 04:49:40

使用 array_filter() 的方法(假设 >= 5.3):

$strings = array('software', 'software', 'game', 'antivirus');

$filtered = array_filter($strings, function($curr) {

    static $prev; 

    similar_text($prev, $curr, $percent);
    $prev = $curr;   

    if ($percent < 83) {
        return $curr;
    }        
});

print_r($filtered);

产量:

Array
(
    [0] => software
    [2] => game
    [3] => antivirus
)

希望这会有所帮助。事实上,直到现在我才知道 similar_text()。相当有趣的功能。谢谢 :)

An approach using array_filter() (assumes >= 5.3):

$strings = array('software', 'software', 'game', 'antivirus');

$filtered = array_filter($strings, function($curr) {

    static $prev; 

    similar_text($prev, $curr, $percent);
    $prev = $curr;   

    if ($percent < 83) {
        return $curr;
    }        
});

print_r($filtered);

Yields:

Array
(
    [0] => software
    [2] => game
    [3] => antivirus
)

Hope this helps. Actually, I never knew about similar_text() until now. Pretty interesting function. Thanks :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文