Two select query results, one of which is content (text) and the other is swearing words. how to count the number of times the content appears in the swearing words.

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

Related Article

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.