the perfect solution can't be figured out, after all, it's slag.
Think of a more ugly way to look at the line:
$str = "1111111111111112b22222222caonima22222222"; $words = [' 2b ', ' Caonima ']; $res = [];foreach ($words as $word) { $ res[$word] = count (Explode ($word, $STR))-1;} return $res;
If it is only to count the number of dirty words, do not require every word of the number of times, before writing a function based on multiple key cutting string
$str = "1111111111111112b22222222caonima22222222"; $words = [' 2b ', ' Caonima '];function many_explode ($cutKey, $string) { $return =[]; $key =0; for ($i =0; $i <strlen ($string), $i + +) { if (In_array ($string [$i], $cutKey)) { $key + +;//current character in Cutkey, Skip, And another line }else{ $return [$key]=isset ($return [$key])? $return [$key]. $string [$i]: $string [$i]; } } return $return;} Return count (Many_explode ($words, $str))-1;
Mixture
1. Create a table of all the swearing words.
2, content context to do full-text indexing.
3, use MySQL's own function to retrieve locate, or you can use like.
$arr = Array (' 2b ', ' fuck '); $con = Array (' content ' = ' asdfasdf2bsdfasdf2dfasdfuck ') , Array (' content ' = ' asdfasdf2asdfasdf2dfasdfuck '), Array (' content ' =&G t; ' 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 Results/**array ([0] = = Array ([content] = Asdfasdf2bsdfasdf2dfasdfuck [Checkcou NT] = Array ([2b] => 1 [Fuck] = 1)) [1] = = Array ([content] = Asdfa Sdf2asdfasdf2dfasdfuck [Checkcount] = Array ([Fuck] = 1 )) [2] = = Array ([content] = Asdfasdfuckbsdfasdf2dfasdfuck [Checkcount] = = Array ([fuck] = 2)) **/
Thank you for answering the answer, learning a variety of writing. Using the first type provided by QVGA, followed by array_sum statistics total. At times thank ^_^