How do you check if a array contains a specific word in php?
This is for a chat page. I have a Show
Except when I do this, when a user types in a word in the
Moozy 4,4073 gold badges17 silver badges18 bronze badges asked Dec 10, 2012 at 6:00
user1879926user1879926 1,2633 gold badges13 silver badges24 bronze badges 3
Robert Went 2,8442 gold badges17 silver badges18 bronze badges answered Dec 10, 2012 at 6:06
Nirav RanparaNirav Ranpara 15.8k4 gold badges42 silver badges57 bronze badges 5 1) The simplest way:
2) Another way (while checking arrays towards another arrays):
answered May 30, 2013 at 12:46
1 can you please try this instead of your code
HeavenCore 7,3856 gold badges46 silver badges59 bronze badges answered Dec 10, 2012 at 7:11
SanjaySanjay 76114 silver badges25 bronze badges 1 You can flip your bad word array and do the same checking much faster. Define each bad word as a key of the array. For example,
In this code we are just checking if an index exist rather than comparing bad word with all the words in the bad word list. answered Dec 3, 2013 at 16:57
1 Put and exit or die once it find any bad words, like this
answered Dec 10, 2012 at 6:07
SanjaySanjay 76114 silver badges25 bronze badges 6 There is a very short php script that you can use to identify bad words in a string which uses str_ireplace as follows:
The single line:
does all the work. answered Jul 21, 2017 at 11:29
ClintonClinton 9731 gold badge11 silver badges19 bronze badges 2 You can do the filter this way also
answered Jun 27, 2019 at 12:56
1 Wanted this?
answered Dec 10, 2012 at 6:11
LeninLenin 57016 silver badges36 bronze badges 2 If you want to do with array_intersect(), then use below code :
answered Aug 17, 2017 at 6:28
Irshad KhanIrshad Khan 5,2902 gold badges42 silver badges38 bronze badges 0 I would go that way if chat string is not that long.
mickmackusa 38.8k11 gold badges76 silver badges111 bronze badges answered Dec 10, 2012 at 6:08
GrabenGraben 2023 silver badges8 bronze badges 3
answered Feb 11, 2015 at 9:15
JasperJasper 3455 silver badges9 bronze badges 2 Not the answer you're looking for? Browse other questions tagged php arrays string compare or ask your own question.How do I find a word in an array PHP?PHP in_array() Function
The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
How do you check if a string contains a word in an array?To check if a string contains a substring from an array:. Use the Array. some() method to iterate over the array.. Check if the string contains each substring.. If the condition is met, the string contains a substring from the array.. How do you search for a word in an array?You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.
How do you check if an array contains an element in PHP?The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.
|