Two select query results, one content (text), the other is a dirty word. How to count the number of occurrences of content in foul words.

Source: Internet
Author: User
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 ^_^

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.