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 8196025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:20:27+00:00 2026-06-07T05:20:27+00:00

i use a php class to make tag cloud from article, but i want

  • 0

i use a php class to make tag cloud from article, but i want to remove words that only 3 character or less, also remove numeric words.

example tags: 1111 monkey deer cat pig buffalo

i want result : monkey deer buffalo

PHP code from that class (full code here)

    function keywords_extract($text)
{
    $text = strtolower($text);
    $text = strip_tags($text);

    /* 
     * Handle common words first because they have punctuation and we need to remove them
     * before removing punctuation.
     */
    $commonWords = "'tis,'twas,a,able,about,across,after,ain't,all,almost,also,am,among,an,and,any,are,aren't," .
        "as,at,be,because,been,but,by,can,can't,cannot,could,could've,couldn't,dear,did,didn't,do,does,doesn't," .
        "don't,either,else,ever,every,for,from,get,got,had,has,hasn't,have,he,he'd,he'll,he's,her,hers,him,his," .
        "how,how'd,how'll,how's,however,i,i'd,i'll,i'm,i've,if,in,into,is,isn't,it,it's,its,just,least,let,like," .
        "likely,may,me,might,might've,mightn't,most,must,must've,mustn't,my,neither,no,nor,not,o'clock,of,off," .
        "often,on,only,or,other,our,own,rather,said,say,says,shan't,she,she'd,she'll,she's,should,should've," .
        "shouldn't,since,so,some,than,that,that'll,that's,the,their,them,then,there,there's,these,they,they'd," .
        "they'll,they're,they've,this,tis,to,too,twas,us,wants,was,wasn't,we,we'd,we'll,we're,were,weren't,what," .
        "what'd,what's,when,when,when'd,when'll,when's,where,where'd,where'll,where's,which,while,who,who'd," .
        "who'll,who's,whom,why,why'd,why'll,why's,will,with,won't,would,would've,wouldn't,yet,you,you'd,you'll," .

    $commonWords = strtolower($commonWords);
    $commonWords = explode(",", $commonWords);
    foreach($commonWords as $commonWord) 
    {
        $text = $this->str_replace_word($commonWord, "", $text);  
    }

    /* remove punctuation and newlines */
    /*
     * Changed to handle international characters
     */
    if ($this->m_bUTF8)
        $text = preg_replace('/[^\p{L}0-9\s]|\n|\r/u',' ',$text);
    else
        $text = preg_replace('/[^a-zA-Z0-9\s]|\n|\r/',' ',$text);

    /* remove extra spaces created */
    $text = preg_replace('/ +/',' ',$text);
    $text = trim($text);
    $words = explode(" ", $text);
    foreach ($words as $value) 
    {
        $temp = trim($value);
        if (is_numeric($temp))
            continue;
        $keywords[] = trim($temp);
    }
    return $keywords;
}

I’ve tried various ways, such as use if (strlen($words)<3 && is_numeric($words)==true), but it did not work.

please help me

  • 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-06-07T05:20:31+00:00Added an answer on June 7, 2026 at 5:20 am

    I will slightly modify your process to make it run faster (I believe it should).

    Step 1: Instead of replace each common word to empty string in $text (replace process is expensive), I will store each common words into hash table for later filter.

    $commonWords = explode(",", $commonWords);
    foreach($commonWords as $commonWord)
        $hashWord[$commonWord] = $commonWord;
    

    Step 2: Filter common word, numeric and words that contain less than 4 digits at the same time.

    $words = preg_split("/[\s\n\r]/", $text);
    foreach ($words as $value) 
    {
        // Skip it is common word
        if (isset($hashWord[$value])) continue;
        // Skip if it is numeric
        if (is_numeric($value)) continue;
        // Skip if word contains less than 4 digits
        if (strlen($value) < 4) continue;
    
        $keywords[] = preg_replace('/[^a-zA-Z0-9\s].+/', '', $value);
    }
    

    The following is a full source code for this function (in case you want to copy and paste)

    function keywords_extract($text) {
        $text = strtolower($text);
        $text = strip_tags($text);
    
        $commonWords = "'tis,'twas,a,able,about,across,after,ain't,all,almost,also,am,among,an,and,any,are,aren't," .
            "as,at,be,because,been,but,by,can,can't,cannot,could,could've,couldn't,dear,did,didn't,do,does,doesn't," .
            "don't,either,else,ever,every,for,from,get,got,had,has,hasn't,have,he,he'd,he'll,he's,her,hers,him,his," .
            "how,how'd,how'll,how's,however,i,i'd,i'll,i'm,i've,if,in,into,is,isn't,it,it's,its,just,least,let,like," .
            "likely,may,me,might,might've,mightn't,most,must,must've,mustn't,my,neither,no,nor,not,o'clock,of,off," .
            "often,on,only,or,other,our,own,rather,said,say,says,shan't,she,she'd,she'll,she's,should,should've," .
            "shouldn't,since,so,some,than,that,that'll,that's,the,their,them,then,there,there's,these,they,they'd," .
            "they'll,they're,they've,this,tis,to,too,twas,us,wants,was,wasn't,we,we'd,we'll,we're,were,weren't,what," .
            "what'd,what's,when,when,when'd,when'll,when's,where,where'd,where'll,where's,which,while,who,who'd," .
            "who'll,who's,whom,why,why'd,why'll,why's,will,with,won't,would,would've,wouldn't,yet,you,you'd,you'll,";
    
        $commonWords = explode(",", $commonWords);
        foreach($commonWords as $commonWord)
            $hashWord[$commonWord] = $commonWord;
    
        $words = preg_split("/[\s\n\r]/", $text);
        foreach ($words as $value) 
        {
            // Skip it is common word
            if (isset($hashWord[$value])) continue;
            // Skip if it is numeric
            if (is_numeric($value)) continue;
            // Skip if word contains less than 4 digits
            if (strlen($value) < 4) continue;
    
            $keywords[] = preg_replace('/[^a-zA-Z0-9\s].+/', '', $value);
        }
        return $keywords;
    }
    

    Demo: ideone.com/obG6n

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make variables and methods in php class. And want to use
How could i use a php die function to only stop certain divs from
I am writing a simple PHP script that make use of DB to export
I have an existing PHP MySQL web app that I want to make an
I'm using this PHP class: http://www.phpclasses.org/browse/file/9524.html I use this code to make it work:
I have turned a normal PHP class into a library so I can use
For class, I would like a to use PHP MVC framework very similar to
How to use oAuth with PHP ? I can't install oauth class with pecl
I try to use PHPMailer to send registration, activation. etc mail to users: require(class.phpmailer.php);
To simulate enums in PHP I like to use class constants. e.g. class FRUIT

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.