It is the number of times of swearing in each content, based on another array. there is no idea after writing it. how should we count it? the perfect solution can't be found out, after all, it is still slag.
Think of an ugly method, just look at it:
$str = "1111111111111112b22222222caonima22222222";$words = ['2b','caonima'];$res = [];foreach($words as $word){ $res[$word] = count(explode($word, $str))-1;}return $res;
If we only count the number of times of swearing, and do not require each number of times of swearing, we have previously written a function to cut strings based on multiple keys.
$ Str = "11111111111112b22222222caonima22222222"; $ words = ['2b ', 'caonima']; function many_explode ($ cutKey, $ string) {$ return = []; $ key = 0; for ($ I = 0; $ I
Do not change dressing
1. create a table for all the foul words.
2. full-text indexing of content context.
3. Use the built-in functions of mysql to search for locate, or you can use like.
$ Arr = array ('2b ', 'fuck'); $ con = array ('content' => 'asdfasdf2bsdfasdf2dfasdfuck '), array ('content' => 'asdfasdf2asdfasdf2dfasdfuck '), array ('content' => 'asdfasdfuckbsdfasdf2dfasdfuck'),); foreach ($ con as $ ck => $ cv) {foreach ($ arr as $ av) {if ($ pos = strpos ($ cv ['content'], $ av ))! = False) {$ cv ['checkcount'] [$ av] = recursionSearch ($ cv ['content'], $ av, $ pos, 1 );}} $ con [$ ck] = $ cv;} print_r ($ con); // function recursionSearch ($ haystack, $ needle, $ offset = 0, $ res = 0) {$ offset + = strlen ($ needle); if ($ pos = strpos ($ haystack, $ needle, $ offset) ==== false) {return $ res ;} else {return recursionSearch ($ haystack, $ needle, $ pos, $ res + 1 );}} // print the result/** Array ([0] => Array ([content] => asdfasdf2bsdfasdf2dfasdfuck [checkCount] => Array ([2b] => 1 [fuck] => 1 )) [1] => Array ([content] => asdfasdf2asdfasdf2dfasdfuck [checkCount] => Array ([fuck] => 1 )) [2] => Array ([content] => asdfasdfuckbsdfasdf2dfasdfuck [checkCount] => Array ([fuck] => 2 )))**/
Thank you for your answers. I have learned a variety of writing methods. The first qvga type is used, followed by array_sum to calculate the total number. Thank you! ^_^