Check for an array of words in a string in php
Discuss coding issues, and scripts related to PHP and MySQL.
-
Marius
- Posts: 107
Check for an array of words in a string in php
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:
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';
}
But if I have many special words in the array, the foreach() slows down the execution speed.
Admin
You could add the spam words into a string, with | as word separator and then use regular expression to check.
Code: Select all
$my_words = 'buy|sell|shop|purchase';
$input_text = 'we can sell your products'; //user will input this text
if(preg_match('#\b(?:'.$my_words.')\b#i', $input_text, $mt)) echo 'Match: '.$mt[0];
else echo 'No match';
Here is an explanation of the (exact) regex built and used above:
Code: Select all
/\b(?:buy|sell|shop|purchase)\b/i
\b match the start of a word, which is either
(?:
buy
| OR
sell
| OR
shop
| OR
purchase
)
\b match the end of a word
i flag to disable case sensitivity
Similar Topics
-
Check if multidimensional array
PHP - MySQL
First post
How can I check in php if an array is multidimensional or not?
I get a JSON from a third-party application and I parse it in php. But sometimes data...
Last post
You can check with two count() functions. If you add a second argument as True it will recursively count the array.
Here is an example:
$arr =[...
-
Push an array into the same array JS
JavaScript - jQuery - Ajax
First post
I'm trying to push an array into the same array in javascript, But it doesn't seem to be working; the third element is added continuously.
Here is...
Last post
You are trying to push the same reference to the array. So, when the array updated later, the array inside the element will be updated as well.
To...
-
Find item in array and set as first index
JavaScript - jQuery - Ajax
First post
Say I have the following array of persons:
const arr =
And I want to find a certain person (name) in this array, and put it as the first...
Last post
You could sort the array with the sort() method.
This approach moves all objects with the wanted 'name' to top.
const arr = ;
let first =...
-
Split an array of objects into separate arrays
JavaScript - jQuery - Ajax
First post
I have an array of objects, and for plotting a graph I need to split it into 3 different arrays. How to do it using JS?
const dataOverview = ;
I...
Last post
It is very simple, you just use the map() function.
var series1 = dataOverview.map(x => x.series1);
var series2 = dataOverview.map(x =>...
-
Remove backslash from string
PHP - MySQL
First post
How can I remove backslashes from string in php?
I tried the following code.
$str ='abc-\123';
$str = stripcslashes($str);
echo $str; //...
Last post
You can use str_ireplace() to remove backslash from string in php, but like in the following code (add two backslashes into the removing argument):...