如何检查两个字符串是否包含相同的字母?

发布于 2024-11-26 11:16:31 字数 290 浏览 2 评论 0原文

$textone = "pate"; //$_GET
$texttwo = "tape";
$texttre = "tapp";

if ($textone ??? $texttwo) {
echo "The two strings contain the same letters";
}
if ($textone ??? $texttre) {
echo "The two strings NOT contain the same letters";
}

我正在寻找什么 if 语句?

$textone = "pate"; //$_GET
$texttwo = "tape";
$texttre = "tapp";

if ($textone ??? $texttwo) {
echo "The two strings contain the same letters";
}
if ($textone ??? $texttre) {
echo "The two strings NOT contain the same letters";
}

What if statement am I looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一场春暖 2024-12-03 11:16:31

我想解决方案可能是考虑以下两个变量:

$textone = "pate";
$texttwo = "tape";

1. 首先,分割字符串,得到两个字母数组:

$arr1 = preg_split('//', $textone, -1, PREG_SPLIT_NO_EMPTY);
$arr2 = preg_split('//', $texttwo, -1, PREG_SPLIT_NO_EMPTY);

请注意,正如 @Mike 在他的评论中指出的那样,而不是使用 preg_split() 就像我第一次做的那样,对于这种情况,最好使用 str_split()

$arr1 = str_split($textone);
$arr2 = str_split($texttwo);

2. 然后,对这些数组进行排序,使字母按字母顺序排列:

sort($arr1);
sort($arr2);

3. 之后,对数组进行内爆,以创建所有字母均按字母顺序排列的单词

$text1Sorted = implode('', $arr1);
$text2Sorted = implode('', $arr2);

4.最后,比较这两个单词

if ($text1Sorted == $text2Sorted) {
    echo "$text1Sorted == $text2Sorted";
}
else {
    echo "$text1Sorted != $text2Sorted";
}


将这个想法转化为比较函数将为您提供以下代码部分:

function compare($textone, $texttwo) {
    $arr1 = str_split($textone);
    $arr2 = str_split($texttwo);

    sort($arr1);
    sort($arr2);

    $text1Sorted = implode('', $arr1);
    $text2Sorted = implode('', $arr2);

    if ($text1Sorted == $text2Sorted) {
        echo "$text1Sorted == $text2Sorted<br />";
    }
    else {
        echo "$text1Sorted != $text2Sorted<br />";
    }
}

并在您的两个单词上调用该函数:

compare("pate", "tape");
compare("pate", "tapp");

将得到以下结果:

aept == aept
aept != appt

I suppose a solution could be to, considering the two following variables :

$textone = "pate";
$texttwo = "tape";

1. First, split the strings, to get two arrays of letters :

$arr1 = preg_split('//', $textone, -1, PREG_SPLIT_NO_EMPTY);
$arr2 = preg_split('//', $texttwo, -1, PREG_SPLIT_NO_EMPTY);

Note that, as pointed out by @Mike in his comment, instead of using preg_split() like I first did, for such a situation, one would be better off using str_split() :

$arr1 = str_split($textone);
$arr2 = str_split($texttwo);

2. Then, sort those array, so the letters are in alphabetical order :

sort($arr1);
sort($arr2);

3. After that, implode the arrays, to create words where all letters are in alphabetical order :

$text1Sorted = implode('', $arr1);
$text2Sorted = implode('', $arr2);

4. And, finally, compare those two words :

if ($text1Sorted == $text2Sorted) {
    echo "$text1Sorted == $text2Sorted";
}
else {
    echo "$text1Sorted != $text2Sorted";
}


Turning this idea into a comparison function would give you the following portion of code :

function compare($textone, $texttwo) {
    $arr1 = str_split($textone);
    $arr2 = str_split($texttwo);

    sort($arr1);
    sort($arr2);

    $text1Sorted = implode('', $arr1);
    $text2Sorted = implode('', $arr2);

    if ($text1Sorted == $text2Sorted) {
        echo "$text1Sorted == $text2Sorted<br />";
    }
    else {
        echo "$text1Sorted != $text2Sorted<br />";
    }
}

And calling that function on your two words :

compare("pate", "tape");
compare("pate", "tapp");

Would get you the following result :

aept == aept
aept != appt
黑凤梨 2024-12-03 11:16:31

使用 ===!==

if ($textone === $texttwo) {
    echo "The two strings contain the same letters";
}else{
    echo "The two strings NOT contain the same letters";
}

if ($textone === $texttwo) {
    echo "The two strings contain the same letters";
}

if ($textone !== $texttwo) {
    echo "The two strings NOT contain the same letters";
}

use === and !==

if ($textone === $texttwo) {
    echo "The two strings contain the same letters";
}else{
    echo "The two strings NOT contain the same letters";
}

or

if ($textone === $texttwo) {
    echo "The two strings contain the same letters";
}

if ($textone !== $texttwo) {
    echo "The two strings NOT contain the same letters";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文