PHP Ifelse statement is true regardless
Ok, I am now trying to expand on a bit of code that I had resolved here previously. 开发者_如何学CThis 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 !!
@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;
}
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:
$keyword
is initially "white"- Next token is "and," so it appends
" "
plus the token"and"
. Now $keyword is"white and"
. - 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".
I think you're checking the wrong var in your elif
.
Try this instead:
elseif ($keyword !== "and" && $keyword !== "or" && $token !== false)
精彩评论