Jump to content

How to ECHO which item was found/matched after running "foreach"


Recommended Posts

 

Hi everyone, php newbe here.  Can someone tell me how I can ECHO which keyword is found/matched when I run this code?

$uri = $_SERVER['REQUEST_URI'];

$spam_keywords = [
    'spamword1',
    'spamword2',
    'spamword3'
// WILL BE A LOT MORE HERE
];

function checkKeywords($uri, $keywords) {
    foreach ($keywords as $keyword) {
        if (strpos($uri, $keyword) !== false) {
            return true;
        }
    }
    return false;
}

if (checkKeywords($uri, $spam_keywords)) {
    // HERE I NEED TO ECHO WHICH KEYWORD WAS MATCHED
    echo "<p>HOW TO ECHO WHICH KEYWORD WAS MATCHED?</p>";
} else {
	echo "keywords not found";
}

 

 

Edited by myphp

Right now, you have the function returning just true/false depending whether it found a match. Perhaps it would be more useful if, instead of returning true for a match, it returned the actual keyword that matched?

2 hours ago, requinix said:

Right now, you have the function returning just true/false depending whether it found a match. Perhaps it would be more useful if, instead of returning true for a match, it returned the actual keyword that matched?

That is what am trying to do but I don't know how to change code below to return which exact $spam_keywords was match

 

    foreach ($keywords as $keyword) {
        if (strpos($uri, $keyword) !== false) {
            return true;
        }
    }

 

Not sure I follow. You have code there that goes through every single keyword in the array, which is known as "$keywords" inside that function and not "$spam_keywords" as it's called outside the function, testing to see if it matches, and then returning true if it does. All you need to change here is to make it return the keyword that matched instead of true.

If you are wanting to know which keyword(s) is/are found, there is one flaw in your logic - it exits the function on the first keyword found. What if there are two or more keywords that are found? Instead of returning true on the first keyword found, I'd suggest adding the found keywords to an array. Then return the array. Also, I would assume you would want to do a case insensitive search. E.g. if you have "spamword1", you would want a match on something like "SpAmWoRd1". If so, you would want to use stripos()

Give a look at this:

$uri = $_SERVER['REQUEST_URI'];

$spam_keywords = [
    'spamword1',
    'spamword2',
    'spamword3'
// WILL BE A LOT MORE HERE
];

function checkKeywords($uri, $keywords) {
    $foundKeywords = array();      //Create array to hold found keywords
    foreach ($keywords as $keyword) {            //Iterate over list of keywords
        if (stripos($uri, $keyword) !== false) { //Check if keyword is in $uri
            $foundKeywords[] = $keyword;         //Add found keyword to array
        }
    }
    //Return found keywords
    return $foundKeywords;
}

//Check for keywords
$foundKeywords = checkKeywords($uri, $spam_keywords);
//Check if results are not empty
if (!empty($foundKeywords)) {
    //Keywords were found - display them
    echo "<p>The following Keywords were found:" . implode(', ', $foundKeywords) . "</p>";
} else {
    //No keywords found
	echo "keywords not found";
}

 

 

  • Great Answer 1

The code Psycho provided is a fairly standard way of solving this problem.

Notice how he made use of the empty() function.  

Empty works well here, because it will return false if it receives an empty array, and true for anything else.

27 minutes ago, gizmola said:

The code Psycho provided is a fairly standard way of solving this problem.

Notice how he made use of the empty() function.  

Empty works well here, because it will return false if it receives an empty array, and true for anything else.

To be honest, I vacillated between several options. Pretty sure all of these would have worked.

if (!empty($foundKeywords)) { 
if ($foundKeywords) {         
if (count($foundKeywords)) {  
if (sizeof($foundKeywords)) {  

Also considere4d having the function returning a Boolean FALSE if no keywords were found instead of an empty array. But decided that an empty array was simpler.

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.