sjaakp / yii2-wordcount
Word count behavior for Yii2.
Installs: 74
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
pkg:composer/sjaakp/yii2-wordcount
Requires
- yiisoft/yii2: *
 
This package is auto-updated.
Last update: 2025-10-26 23:39:02 UTC
README
Word count behavior for Yii2
This is a word counting behavior for ActiveRecords in the Yii 2.0 PHP Framework. It counts the words in one or more designated attributes. The count(s) are exposed through new virtual attributes.
A demonstration of yii2-wordcount is here.
Installation
The preferred way to install yii2-wordcount is through Composer.
Either add the following to the require section of your composer.json file:
"sjaakp/yii2-wordcount": "*"
Or run:
composer require sjaakp/yii2-wordcount "*"
You can manually install yii2-wordcount by downloading the source in ZIP-format.
Using WordCount
WordCount is a Behavior for an ActiveRecord. It has one property:
- $attribute 
string|arrayThe name of the attribute of wihich we want to count the words. Can also be an array of attribute names. Moreover, it can be an array with'<attrName>' => '<countAttrName>'elements. 
If the count attribute name is not explicitly set in the $attribute array, the virtual count attribute
is called '<attrName>_count' automatically.
Here is the simplest way to set up an ActiveRecord with WordCount:
namespace app\models;
use yii\db\ActiveRecord;
use sjaakp\wordcount\WordCount;
class Article extends ActiveRecord
{
    public static function tableName()
    {
        return 'article';
    }
    // ...
        
    public function behaviors()
    {
        return [
            [
                'class' => WordCount::class,
                'attribute' => 'bodytext'
            ],
            // ... other behaviors ...
        ];
    }
    // ...
}
Class Article will now have a new virtual attribute with the name 'bodytext_count'.
It's value is an integer and it can be queried just like any other attribute:
$wordsCounted = $model->bodytext_count
A slightly more involved way to set up an Activerecord with WordCount would be:
 // ...     
 class Article extends ActiveRecord
 {
     // ...
         
     public function behaviors()
     {
         return [
             [
                 'class' => WordCount::class,
                 'attribute' => [
                    'bodytext' => 'textcount',
                    'title' => 'titlecount'
                 ]
             ],
             // ... other behaviors ...
         ];
     }
     // ...
 }
It gives two new virtual attributes, named 'textcount' and 'titlecount'.
Notice that WordCount uses the PHP function
str_word_count().
This is not the most perfect way to count words, so you should consider the results
as no more than good approximations.
Totals
Totals is a helper class with one method:
public static function count($query, $attribute)
This static function returns the total of $attribute values in the ActiveRecords
found by ActiveQuery
$query. If $attribute is a string, the return value will be an integer.
If $attribute is an array of attribute names, count() will return an array
with '<attr>' => <total> elements.
Usage example:
use sjaakp\wordcount\Totals;
$totals = Totals::count(Article::find(), [ 'titlecount', 'textcount' ]);
Notice that count() also works with non-virtual attributes. However, it would
be much wiser to use ActiveQuery::sum()
in that case.