php - 为什么我没有得到相同的输出?

标签 php strstr

我正在尝试检查字符串是否为 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/

相关文章:

php - 在 php 中使用 strstr 作为 boolean 检查是什么意思?

c - 奇怪的 strstr 行为

php - 检查 WooCommerce 优惠券是否已存在

javascript - 阅读并显示一个word文档

php - 在使用 jquery 进行 php 计算期间通知用户

c - 搜索文件时发生访问冲突

c - 当strstr找到指定字符串时如何增加变量

php - 查询两个单独的表,在第二个查询中传递结果,而如果第二个查询返回 null,则仅保留第一个查询的结果

php - 使用 CakePhp 的自动脚本

c - strstr 与 c 中的正则表达式