如何检查两个字符串是否包含相同的字母?
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想解决方案可能是考虑以下两个变量:
1. 首先,分割字符串,得到两个字母数组:
请注意,正如 @Mike 在他的评论中指出的那样,而不是使用
preg_split()
就像我第一次做的那样,对于这种情况,最好使用str_split()
:2. 然后,对这些数组进行排序,使字母按字母顺序排列:
3. 之后,对数组进行内爆,以创建所有字母均按字母顺序排列的单词:
4.最后,比较这两个单词:
将这个想法转化为比较函数将为您提供以下代码部分:
并在您的两个单词上调用该函数:
将得到以下结果:
I suppose a solution could be to, considering the two following variables :
1. First, split the strings, to get two arrays of letters :
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 usingstr_split()
:2. Then, sort those array, so the letters are in alphabetical order :
3. After that, implode the arrays, to create words where all letters are in alphabetical order :
4. And, finally, compare those two words :
Turning this idea into a comparison function would give you the following portion of code :
And calling that function on your two words :
Would get you the following result :
使用
===
和!==
或
use
===
and!==
or