Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7457971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:23:24+00:00 2026-05-29T13:23:24+00:00

This php function retrieves a list of common words used in a string and

  • 0

This php function retrieves a list of common words used in a string and excludes a blacklist of words.

Array1: a,b,c

Although a default blacklist is useful, I needed to add words to the blacklist from a database.

Array2: d,e,f

I added the MYSQL which gets an additional list from an field in our services table.
I explode \n from the words into an array and merge the two arrays at the beginning of the function so that the blacklist is now

Array3: a,b,c,d,e,f

To test I used print_r to display the array and it does merge successfully.

The problem is this…

If I manually add d,e,f to the default array the script returns a clean list of words.
If I merge the two arrays into one its returning the list of words with the blacklist words still in it.

Why would the merged array be any different than just adding to the default array?

Here is the function

function extractCommonWords($string,$init_blacklist){

    /// the default blacklist words
    $stopWords = array('a','b','c');

    /// select the additional blacklist words from the database
    $gettingblack_sql = "SELECT g_serv_blacklist FROM services WHERE g_serv_id='".$init_blacklist."' LIMIT 1";
    $gettingblack_result = mysql_query($gettingblack_sql) or die(mysql_error());
    $gettingblack_row = mysql_fetch_array($gettingblack_result);
    $removingblack_array = explode("\n", $gettingblack_row["g_serv_blacklist"]);

    // this adds the d,e,f array from the database to the default a,b,c blacklist
    $stopWords = array_merge($stopWords,$removingblack_array);

    // replace whitespace
    $string = preg_replace('/\s\s+/i', '', $string); 
    $string = trim($string);

    // only take alphanumerical chars, but keep the spaces and dashes too
    $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); 

    // make it lowercase
    $string = strtolower($string); 

    preg_match_all('/\b.*?\b/i', $string, $matchWords);
    $matchWords = $matchWords[0];

    foreach ($matchWords as $key => $item) {
    if ($item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3){
    unset($matchWords[$key]);}}

    $wordCountArr = array();

    if (is_array($matchWords)) {
        foreach ($matchWords as $key => $val) {
            $val = strtolower($val);
            if (isset($wordCountArr[$val])) {
                $wordCountArr[$val]++;
            } else {
                $wordCountArr[$val] = 1;
            }
        }
    }
    arsort($wordCountArr);
    $wordCountArr = array_slice($wordCountArr, 0, 30);
    return $wordCountArr;
}
/// end of function



    /// posted string =  a b c d e f g
    $generate = $_POST["generate"];

    /// the unique id of the row to retrieve additional blacklist keywords from
    $generate_id = $_POST["generate_id"];

    /// run the function by passing the text string and the id 
    $generate = extractCommonWords($generate, $generate_id);

    /// update the database with the result
    $update_data = "UPDATE services SET 
    g_serv_tags='".implode(',', array_keys($generate))."' 
    WHERE g_serv_acct='".$_SESSION["session_id"]."' 
    AND g_serv_id='".$generate_id."' LIMIT 1";
    $update_result = mysql_query($update_data);
    if(!$update_result){die('Invalid query:' . mysql_error());}
    else{echo str_replace(",",", ",implode(',', array_keys($generate)));}
    /// end of database update
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:23:24+00:00Added an answer on May 29, 2026 at 1:23 pm

    If the extra blacklist in the database was populated in an admin panel from a Windows client, there is likely to be a stray \r at the end of each word. Thus, your list would be a,b,c,d\r,e\r,f\r.

    Try replacing this line:

    $removingblack_array = explode("\n", $gettingblack_row["g_serv_blacklist"]);
    

    with this:

    $removingblack_array = preg_split('/(\r|\n|\r\n)/', $gettingblack_row["g_serv_blacklist"]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I this PHP function include in jquery .html? PHP function: function trim_text($string,
If this was PHP, I would probably do something like this: function no_more_half_widths($string){ $foo
I currently call a function to filter some HTML in PHP like this FilterHTML($string)
I'm getting this problem: PHP Warning: mail() [function.mail]: SMTP server response: 550 5.7.1 Unable
This example is from php.net: <?php function Test() { static $a = 0; echo
i'm referring this address for function olLiTree PHP function that creates a nested ul
How do you translate this perl subroutine into a PHP function? sub disagreement {
I have a JavaScript function, pop_item . I have to call this from PHP,
This question is regarding getopt function in php. I need to pass two parameter
This is my jquery function: $(function() { $(#user).autocomplete({ source: 'spiderman/null/users.php', minLength: 2 }); });

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.