Php check if string contains words in array
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,4173 gold badges17 silver badges18 bronze badges asked Dec 10, 2012 at 6:00
3
Robert Went 2,8442 gold badges17 silver badges18 bronze badges answered Dec 10, 2012 at 6:06
Nirav RanparaNirav Ranpara 15.8k4 gold badges43 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
T.ToduaT.Todua 50.1k19 gold badges216 silver badges213 bronze badges 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,3402 gold badges42 silver badges38 bronze badges 0 I would go that way if chat string is not that long.
mickmackusa 39k11 gold badges76 silver badges112 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 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 check if a array contains a specific word in PHP?Answer: Use the PHP strpos() Function
You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string.
How do you find if an array contains a specific string in PHP?Approach: In order to search an array for a specific value, we will be using the in_array() function where the parameter for the search is of string type & its value is set to true. Otherwise, this function returns a false value if the specified value is not found in an array.
How do you check if a string contains a substring PHP?You can use the PHP strpos() function to get the position of the first occurrence of a substring in a string. The function will return false if it cannot find the substring.
|