PHPackages                             sjaakp/yii2-wordcount - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. sjaakp/yii2-wordcount

ActiveYii2-extension

sjaakp/yii2-wordcount
=====================

Word count behavior for Yii2.

1.0.1(6y ago)087MITPHP

Since Jan 25Pushed 6y ago1 watchersCompare

[ Source](https://github.com/sjaakp/yii2-wordcount)[ Packagist](https://packagist.org/packages/sjaakp/yii2-wordcount)[ RSS](/packages/sjaakp-yii2-wordcount/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (1)Versions (2)Used By (0)

Yii2-wordcount
--------------

[](#yii2-wordcount)

#### Word count behavior for Yii2

[](#word-count-behavior-for-yii2)

[![Latest Stable Version](https://camo.githubusercontent.com/7ee7ee3aef953519ab9e3769f69246323b1296240d47a6499475fc5a50066bf4/68747470733a2f2f706f7365722e707567782e6f72672f736a61616b702f796969322d776f7264636f756e742f762f737461626c65)](https://packagist.org/packages/sjaakp/yii2-wordcount)[![Total Downloads](https://camo.githubusercontent.com/0a4852c484d507bb47271a21afd6eb33a786a43efc7361fc4595fd9d78eea16e/68747470733a2f2f706f7365722e707567782e6f72672f736a61616b702f796969322d776f7264636f756e742f646f776e6c6f616473)](https://packagist.org/packages/sjaakp/yii2-wordcount)[![License](https://camo.githubusercontent.com/691def69cd8231cdca2d56c6b9fc328357e192aaca3b4eb431ce3a1146c7f0d7/68747470733a2f2f706f7365722e707567782e6f72672f736a61616b702f796969322d776f7264636f756e742f6c6963656e7365)](https://packagist.org/packages/sjaakp/yii2-wordcount)

This is a word counting behavior for [ActiveRecords](https://www.yiiframework.com/doc/api/2.0/yii-db-activerecord)in the [Yii 2.0](https://yiiframework.com/ "Yii") PHP Framework. It counts the words in one or more designated attributes. The count(s) are exposed through new [virtual attributes](https://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-getset-methods).

A demonstration of **yii2-wordcount** is [here](https://sjaakpriester.nl/software/wordcount).

Installation
------------

[](#installation)

The preferred way to install **yii2-wordcount** is through [Composer](https://getcomposer.org/). 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](https://github.com/sjaakp/yii2-wordcount/archive/master.zip).

Using WordCount
---------------

[](#using-wordcount)

**WordCount** is a [Behavior](https://www.yiiframework.com/doc/api/2.0/yii-base-behavior)for an [ActiveRecord](https://www.yiiframework.com/doc/api/2.0/yii-db-activerecord). It has one property:

- **$attribute** `string|array` The 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 `'' => ''` elements.

If the count attribute name is not explicitly set in the `$attribute` array, the virtual count attribute is called `'_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()`](https://www.php.net/manual/en/function.str-word-count.php). This is not the most perfect way to count words, so you should consider the results as no more than good approximations.

Totals
------

[](#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](https://www.yiiframework.com/doc/api/2.0/yii-db-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 `'' => ` 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()`](https://www.yiiframework.com/doc/api/2.0/yii-db-query#sum()-detail)in that case.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2299d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/156f636aceedc73021a2bfafb5433271c2c55d6cf4bce06b12902c85b4b491f1?d=identicon)[sjaakp](/maintainers/sjaakp)

---

Top Contributors

[![sjaakp](https://avatars.githubusercontent.com/u/5585878?v=4)](https://github.com/sjaakp "sjaakp (3 commits)")

---

Tags

yii2extensionBehavioryiiword count

### Embed Badge

![Health badge](/badges/sjaakp-yii2-wordcount/health.svg)

```
[![Health](https://phpackages.com/badges/sjaakp-yii2-wordcount/health.svg)](https://phpackages.com/packages/sjaakp-yii2-wordcount)
```

###  Alternatives

[sjaakp/yii2-sortable-behavior

Sort ActiveRecords and related records in Yii2.

36144.7k](/packages/sjaakp-yii2-sortable-behavior)[sjaakp/yii2-taggable

Manage tags of ActiveRecord in Yii2.

3030.6k](/packages/sjaakp-yii2-taggable)[nanson/yii2-postgis

Yii2-extension to work with postgis data

1851.6k](/packages/nanson-yii2-postgis)[sjaakp/yii2-illustrated-behavior

ActiveRecord Behavior with associated Widget for Yii2.

423.1k](/packages/sjaakp-yii2-illustrated-behavior)[valentinek/yii2-closure-table-behavior

This extension allows you to get functional for closure table trees.

1615.3k](/packages/valentinek-yii2-closure-table-behavior)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
