PHP Ifelse 语句无论如何都为 true

发布于 2024-11-03 19:42:52 字数 2325 浏览 2 评论 0原文

好的,我现在尝试扩展我之前在这里解决的一些代码。 下面的代码分配在搜索框中输入的短语,并尝试执行以下操作: 1)清理掉我不想要的东西(注意:输入是经过预处理的,所以我现在只是放弃更多,并且仍在处理这部分)。 2)使用空格作为分隔符将短语分成单独的单词 3) 检查提取的每个单词,看看前 3 个字符中是否存在 *,如果存在,则中止该过程 4)检查是否使用了单词“and”或“or” - 如果是,则不加修改地应用它们 - 如果没有,则在短语中插入“and”。 (如果用户自己没有指定,最终会自动将该短语转换为“and”短语)。 5)在整个过程中,$keyword 被重建,最后我回显最终的新 $keyword 来测试结果......当然,接下来是更多代码,然后利用该新关键字。

if ($keyword)  {

            $clean = preg_replace('/[^a-zA-Z0-9 *_&]/','',$keyword);            
            $token = strtok($clean, " ");

            $keyword = $token;

            while ($token !== false) {
                $pos = stripos($token, "*");
                if ($pos < 3 && $pos !== false) {
                return;
                }
            $token = strtok(" ");
                if ($token == "and" || $token == "or") {
                    $keyword = $keyword . " " . $token; 
                } elseif ($token) {
                    $keyword = $keyword . " and " . $token;
                }
            }   
            echo $keyword;

问题:

一切工作正常,除了由于某种原因 ELSEIF 语句始终为真?!无论我做什么,都会插入一个额外的“and”,无论上面的 if 语句是否为真。我已经验证了最初的 if 语句确实有效,它检测“and”或“or”是否存在并相应地应用它......但无论如何它都会继续处理 ELSEIF!我什至尝试过:

elseif ($token !== "and" && $token !== "or" && $token !== false) 

但最终,结果短语以 'and and's 或 'or and's 结尾,无论如何。

(注意:我意识到有比 preg_replace 更好的选择,但我会在其他时间研究这个问题 - 所以对于这个问题,我只是想解决 ELSEIF 困境,谢谢)


附加 MODS 基于响应...

所以,我将代码更改为...

$token = strtok(" ");
                if (in_array($token, array( 'and', 'or' ))) {
                    $keyword = $keyword . " " . $token; 
                } elseif (!empty($token)) {
                    $keyword = $keyword . " and " . $token;
                }

但结果仍然不正确。例如:

“whitefootballhelmut”确实变成了“whiteandfootballandhelmut”, 然而... “白色和足球和赫尔穆特”变成“白色和足球和赫尔穆特”。

我只是不明白如果 if 为 true 则如何处理 ifelse?

注意:为了验证 IF 部分是否正常工作,我在该语句中放置了一个 x:

if (in_array($token, array( 'and', 'or' ))) {
                    $keyword = $keyword . " x" . $token; 

“white and Football and helmut”结果为“white xand and Football xand and helmut”。此外,“white and Football helmut”(无第 2 个和)会生成“white xand and Football and helmut”。 !!因此,IF 语句正在按预期进行处理 - 只是不是 ELSEIF!

Ok, I am now trying to expand on a bit of code that I had resolved here previously.
This code below assigns a phrase entered into a search box, and attempts to do the following:
1) clean out stuff I don't want in there (NOTE: The input is pre-processed so I am just stipping more off for now, and still working on this part).
2) break the phrase into individual words using spaces as the separator
3) check each individual word extracted to see if a * exists within the first 3 characters, and if so, abort the process
4) check if the words "and" or "or" are used - if so, apply them with no modification - if not, then insert an "and" to the phrase. (Ultimately converting the phrase to an "and" phrase automatically if the user doesn't specify it themselves).
5) Throughout the process, $keyword is reconstructed, and at the end I am echoing the final, new $keyword to test the result... of course more code follows then utilizing that new keyword.

if ($keyword)  {

            $clean = preg_replace('/[^a-zA-Z0-9 *_&]/','',$keyword);            
            $token = strtok($clean, " ");

            $keyword = $token;

            while ($token !== false) {
                $pos = stripos($token, "*");
                if ($pos < 3 && $pos !== false) {
                return;
                }
            $token = strtok(" ");
                if ($token == "and" || $token == "or") {
                    $keyword = $keyword . " " . $token; 
                } elseif ($token) {
                    $keyword = $keyword . " and " . $token;
                }
            }   
            echo $keyword;

PROBLEM:

Everything is working fine, except for some reason the ELSEIF statement is ALWAYS TRUE?! No matter what I do, an extra ' and' is inserted, regardless of whether the if statement above it is true or not. I have verified that the initial if statement does work, it detects whether 'and' or 'or' exists and applies it accordingly... but then it goes ahead and processes the ELSEIF anyways!! I have even gone so far as trying:

elseif ($token !== "and" && $token !== "or" && $token !== false) 

But in the end, the resulting phrase ends up with 'and and's or 'or and's, regardless.

(Note: I realize there are better options than preg_replace but I will be looking into that at another time - so for this question, I am just trying to resolve the ELSEIF dilemma, thanks)


APPENDING MODS Based on responses...

So, I changed the code to...

$token = strtok(" ");
                if (in_array($token, array( 'and', 'or' ))) {
                    $keyword = $keyword . " " . $token; 
                } elseif (!empty($token)) {
                    $keyword = $keyword . " and " . $token;
                }

But the results are still incorrect. For example:

"white football helmut" does become "white and football and helmut",
however...
"white and football and helmut" becomes "white and and football and and helmut".

I just don't see how the ifelse can be processing if the if is true??

Note: To verify that the IF portion is working, I placed an x in that statement:

if (in_array($token, array( 'and', 'or' ))) {
                    $keyword = $keyword . " x" . $token; 

And "white and football and helmut" results in "white xand and football xand and helmut". Also, "white and football helmut" (no 2nd and) results in "white xand and football and helmut". !! So, the IF statement is processing as expected - just not the ELSEIF !!

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

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

发布评论

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

评论(3

孤云独去闲 2024-11-10 19:42:52

@Soyo:尝试使用这个 -

if ($token == 'and' || $token == 'or') {
    // do nothing here to avoid duplicating 'and'
}
elseif (!empty($token)) {
    $keyword = $keyword . ' and ' . $token;
}

更新

这是我用来测试的脚本 -

$keyword = 'white and and or and football and or helmut and';

if ($keyword)
{
    $clean   = preg_replace('/[^a-zA-Z0-9 *_&]/', '', $keyword);
    $token   = strtok($clean, ' ');
    $keyword = $token;

    while ($token !== false) 
    {
        $pos = stripos($token, '*');

        if ($pos < 3 && $pos !== false)
        {
            return;
        }
        $token = strtok(' ');

        if (in_array(strtolower($token), array('and', 'or'))) {
            // do nothing here!
        } elseif (!empty($token)) {
            $keyword = $keyword . ' and ' . $token;
        }
    }
    echo $keyword;
}

@Soyo: Try with this --

if ($token == 'and' || $token == 'or') {
    // do nothing here to avoid duplicating 'and'
}
elseif (!empty($token)) {
    $keyword = $keyword . ' and ' . $token;
}

Update

This is the script I use to test --

$keyword = 'white and and or and football and or helmut and';

if ($keyword)
{
    $clean   = preg_replace('/[^a-zA-Z0-9 *_&]/', '', $keyword);
    $token   = strtok($clean, ' ');
    $keyword = $token;

    while ($token !== false) 
    {
        $pos = stripos($token, '*');

        if ($pos < 3 && $pos !== false)
        {
            return;
        }
        $token = strtok(' ');

        if (in_array(strtolower($token), array('and', 'or'))) {
            // do nothing here!
        } elseif (!empty($token)) {
            $keyword = $keyword . ' and ' . $token;
        }
    }
    echo $keyword;
}
无边思念无边月 2024-11-10 19:42:52

您需要检查当前和之前的标记都不是“and”/“or”。

$prevToken = $token;
$token = strtok(" ");
    if ($token == "and" || $token == "or"
       || $prevToken == "and" || $prevToken == "or" 
    ) {
        $keyword = $keyword . " " . $token; 
    } elseif ($token) {
        $keyword = $keyword . " and " . $token;
    }

使用“white and Football helmut”,目前的逻辑如下:

  1. $keyword 最初是“white”,
  2. 下一个标记是“and”,因此它会附加 " " 加上标记“和”。现在$keyword是“white and”
  3. 下一个标记是“football”,因此它会附加 " 和 " 以及 "football"。现在$keyword是“白色和足球”

这就是你得到双重“和”的方式。检查当前和以前的标记以避免连续的“和”/“或”。

You need to check that both the current and previous tokens aren't "and"/"or".

$prevToken = $token;
$token = strtok(" ");
    if ($token == "and" || $token == "or"
       || $prevToken == "and" || $prevToken == "or" 
    ) {
        $keyword = $keyword . " " . $token; 
    } elseif ($token) {
        $keyword = $keyword . " and " . $token;
    }

Using "white and football helmut", the logic currently goes like this:

  1. $keyword is initially "white"
  2. Next token is "and," so it appends " " plus the token "and". Now $keyword is "white and".
  3. Next token is "football," so it appends " and " plus "football". Now $keyword is "white and and football".

So that's how you're getting double "and". Check both current and previous tokens to avoid consecutive "and"/"or".

撧情箌佬 2024-11-10 19:42:52

我认为您在 elif 中检查了错误的变量。

试试这个:

elseif ($keyword !== "and" && $keyword !== "or" && $token !== false)

I think you're checking the wrong var in your elif.

Try this instead:

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