phpsane Posted September 2, 2017 Share Posted September 2, 2017 (edited) Hi, The following code seems to be echoing the array numbers. I actually need to echo the array value and not the numbers. Echo: Values of the matched banned words. Echo: Values of the non-matched banned words. So, how to correct the following code which shows results like this: Script 5a - No Match: (0)Script 5a - Banned: array(1)Script 5a - Banned: array(2)Script 5a - Banned: array(3)Script 5a - Banned: array(4) NOTE: "0" is being shown as "no match". But how come when "0" exists both in the content and banned words array ? <?php //script 5d: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string $banned_words = array("0","1","2","3","4"); $content = "0,1,2,3,4,5,6,7,8,9"; for($i=0; $i < count($banned_words); $i++) { if(strrpos($content, $banned_words[$i]) != FALSE ) { echo "Script 5a - Banned: array($banned_words[$i])<br>"; // Place "break;" if you want it to stop after finding a match. }else{ echo "Script 5a - No Match: ($banned_words[$i])<br>"; } // Why is it echoing the array numbers instead of the array values ? } ?> Edited September 2, 2017 by phpsane Quote Link to comment https://forums.phpfreaks.com/topic/304846-how-to-echo-array-value/ Share on other sites More sharing options...
phpsane Posted September 2, 2017 Author Share Posted September 2, 2017 Nor this works like I want: <?php /* ERROR HANDLING */ declare(strict_types=1); ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); //script 5b: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string $banned_words = array("0","1","2","3","4"); $content = "0,1,2,3,4,5,6,7,8,9"; $banned = false; for($i=0; $i < count($banned_words); $i++) { if(strrpos($content, $banned_words[$i]) != FALSE ) { $banned_words = true; echo "Script 5b - Banned:<br>"; print_r($banned_words); // Place "break;" if you want it to stop after finding a match. }else{ echo "Script 5a - No Match: $ban<br>"; print_r($banned_words); } // Why I get error that this closing bracket is unexpected when it is the closing bracket for the ELSE ? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/304846-how-to-echo-array-value/#findComment-1550625 Share on other sites More sharing options...
Solution kosticn101 Posted September 3, 2017 Solution Share Posted September 3, 2017 (edited) "0" is being shown as "no match". But how come when "0" exists both in the content and banned words array ? Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. http://php.net/manual/en/function.strrpos.php So, you need to use !== instead of !=. Why is it echoing the array numbers instead of the array values ?Maybe because array values are array numbers? Why I get error that this closing bracket is unexpected when it is the closing bracket for the ELSE ?I don't get that error by I do get: Notice: Undefined variable: ban in D:\wamp64\www\test.php on line 26 Edited September 3, 2017 by kosticn101 1 Quote Link to comment https://forums.phpfreaks.com/topic/304846-how-to-echo-array-value/#findComment-1550638 Share on other sites More sharing options...
maxxd Posted September 3, 2017 Share Posted September 3, 2017 You're assigning number values to the array indices. So, the script is printing the array values - they just happen to be numbers. Change $banned_words = array("0","1","2","3","4"); to $banned_words = array("PBR","quinoa","kale","portland","beard"); Then head over to https://hipsum.co/ and grab some sample for $content. 1 Quote Link to comment https://forums.phpfreaks.com/topic/304846-how-to-echo-array-value/#findComment-1550642 Share on other sites More sharing options...
phpsane Posted September 3, 2017 Author Share Posted September 3, 2017 So, you need to use !== instead of !=. Maybe because array values are array numbers? I don't get that error by I do get: Notice: Undefined variable: ban in D:\wamp64\www\test.php on line 26 Thanks for pointing out the: !==. Yes, I figured what I was doing wrong, after I made that post. I was getting numbers as values because the array values were numbers. Silly me! Quote Link to comment https://forums.phpfreaks.com/topic/304846-how-to-echo-array-value/#findComment-1550659 Share on other sites More sharing options...
phpsane Posted September 3, 2017 Author Share Posted September 3, 2017 You're assigning number values to the array indices. So, the script is printing the array values - they just happen to be numbers. Change $banned_words = array("0","1","2","3","4"); to $banned_words = array("PBR","quinoa","kale","portland","beard"); Then head over to https://hipsum.co/ and grab some sample for $content. Is this your signature: Then head over to https://hipsum.co/ and grab some sample for $content. I did not realize it first and headed over to the link and got puzzled what it was about. That was a clever tactic on your part to make your post readers click the link. If you're into finding tactics to generate clicks then I'm in the same boat. Keep in touch and PM me whenever you come-up with any manual fraudulent-free click generating tactics. I might attempt to build a php script and a .exe bot around it. We can start a joint venture. Again, yes. I figured what I was doing wrong, after I made that post. I was getting numbers as values because the array values were numbers. Silly me! Quote Link to comment https://forums.phpfreaks.com/topic/304846-how-to-echo-array-value/#findComment-1550661 Share on other sites More sharing options...
kicken Posted September 3, 2017 Share Posted September 3, 2017 Is this your signature: Then head over to https://hipsum.co/ and grab some sample for $content. No, it is not. Signatures are separated and have lighter-color text (see mine below). They were simply suggesting that instead of what you're currently doing, you should grab a paragraph or two of random text and use that as your $content value. You could then pick a few words that are in or not in those paragraphs to use in your $banned_words array. Perhaps then you wouldn't get confused by all the numbers as you'd have distinct words to look at. 1 Quote Link to comment https://forums.phpfreaks.com/topic/304846-how-to-echo-array-value/#findComment-1550666 Share on other sites More sharing options...
maxxd Posted September 5, 2017 Share Posted September 5, 2017 Thanks, kicken - that's exactly what I meant. I have no affiliation with hipster ipsum, I just find it fun to use instead of regular lorem ipsum when I'm populating something with dummy content. Quote Link to comment https://forums.phpfreaks.com/topic/304846-how-to-echo-array-value/#findComment-1550743 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.