我正在尝试检查字符串是否为 PANGRAM,但两个代码给出了不同的结果。
第一 -
<?php
$line = strtolower(trim("thequickbrownfoxjumpsoverthelazydog"));
$letters = str_split("thequickbrownfoxjumpsoverthelazydog");
$result = "pangram";
foreach ($letters as $value) {
if (strstr($line, $value) == FALSE) {
$result = "not pangram";
}
}
echo $result;
?>
第二名 -
<?php
$line = strtolower(trim("thequickbrownfoxjumpsoverthelazydog"));
$letters = str_split("thequickbrownfoxjumpsoverthelazydog");
$result = "not pangram";
foreach ($letters as $value) {
if (strstr($line, $value) == TRUE) {
$result = "pangram";
}
}
echo $result;
?>
最佳答案
非常简单的全字词检查代码
$string = "The quick brown fox jumps over the lazy dog";
$stringData = str_split(strtolower($string));
$check = range('a', 'z');
$res = array_intersect($check, $stringData);
if(count($check) === count($res)) echo 'panagram';
else echo 'not panagram';
就是这样
关于php - 为什么我没有得到相同的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33132611/