myphp Posted December 15 Share Posted December 15 (edited) 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 December 15 by myphp Quote Link to comment https://forums.phpfreaks.com/topic/326186-how-to-echo-which-item-was-foundmatched-after-running-foreach/ Share on other sites More sharing options...
requinix Posted December 15 Share Posted December 15 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? Quote Link to comment https://forums.phpfreaks.com/topic/326186-how-to-echo-which-item-was-foundmatched-after-running-foreach/#findComment-1645923 Share on other sites More sharing options...
myphp Posted December 15 Author Share Posted December 15 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/326186-how-to-echo-which-item-was-foundmatched-after-running-foreach/#findComment-1645926 Share on other sites More sharing options...
requinix Posted December 15 Share Posted December 15 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. Quote Link to comment https://forums.phpfreaks.com/topic/326186-how-to-echo-which-item-was-foundmatched-after-running-foreach/#findComment-1645929 Share on other sites More sharing options...
myphp Posted December 15 Author Share Posted December 15 (edited) So I have to change this: return true; To what? . Edited December 15 by myphp Quote Link to comment https://forums.phpfreaks.com/topic/326186-how-to-echo-which-item-was-foundmatched-after-running-foreach/#findComment-1645933 Share on other sites More sharing options...
Barand Posted December 15 Share Posted December 15 1 hour ago, requinix said: All you need to change here is to make it return the keyword that matched instead of true. Quote Link to comment https://forums.phpfreaks.com/topic/326186-how-to-echo-which-item-was-foundmatched-after-running-foreach/#findComment-1645937 Share on other sites More sharing options...
Psycho Posted December 17 Share Posted December 17 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"; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/326186-how-to-echo-which-item-was-foundmatched-after-running-foreach/#findComment-1646032 Share on other sites More sharing options...
gizmola Posted Wednesday at 09:46 PM Share Posted Wednesday at 09:46 PM 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. Quote Link to comment https://forums.phpfreaks.com/topic/326186-how-to-echo-which-item-was-foundmatched-after-running-foreach/#findComment-1646098 Share on other sites More sharing options...
Psycho Posted Wednesday at 10:24 PM Share Posted Wednesday at 10:24 PM 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. Quote Link to comment https://forums.phpfreaks.com/topic/326186-how-to-echo-which-item-was-foundmatched-after-running-foreach/#findComment-1646100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.