Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4537758
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:43:07+00:00 2026-05-21T14:43:07+00:00

Ok, I am now trying to expand on a bit of code that I

  • 0

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 !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:43:07+00:00Added an answer on May 21, 2026 at 2:43 pm

    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”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am now trying to understand some code and I have found a pattern,
I asked this question earlier and am now trying to explore the idea of
I've added some expand/collapse functionality to a table with jQuery and I'm now trying
As a follow up tho this and this other question, I am now trying
I'm trying to get JScrollPane to reinitialize on expand/collapse of my accordion found here
I am trying to make my text-fields expand when the container does. This is
I am now trying to implement a page with Angularjs. But the problem is
I've spent days now trying to sort out my deps in Symfony 2.0.15. I
I am now trying to create new database manager under Fragment class. But unfortunately,
I am now trying to do forking in php. I would like to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.