Check for an array of words in a string in php
Posted: 18 Nov 2020, 12:40
I have a list of spam words that are into an array. When a user submits a string text, I want to know if it contains these words. How can I do this efficiently?
I wrote this code:
But if I have many special words in the array, the foreach() slows down the execution speed.
I wrote this code:
Code: Select all
$my_words = 'buy;sell;shop;purchase';
$array_words = explode(';' ,$my_words); //convert the list to an array
$input_text = 'we can sell your products'; //user will input this text
foreach($array_words as $word){
if(strpos($input_text ,$word) !== false) return 'You can't use from this text';
}