PHPackages                             apemsel/attributed-string - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. apemsel/attributed-string

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

apemsel/attributed-string
=========================

A collection of fast and easy to use attributed string classes for PHP. Attributed strings can have multiple attributes for each character of the string and are for example used for word processors and natural language processing.

v3.0.0(2y ago)1555MITPHPPHP &gt;=8.1CI failing

Since Feb 20Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/apemsel/AttributedString)[ Packagist](https://packagist.org/packages/apemsel/attributed-string)[ Docs](http://github.com/apemsel/AttributedString)[ RSS](/packages/apemsel-attributed-string/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (10)Dependencies (1)Versions (26)Used By (0)

AttributedString
================

[](#attributedstring)

A collection of classes to work with attributed strings in PHP. Attributed strings are strings that can have multiple attributes per character of the string. Each attribute is a bitmap or boolean array with the length of the string. This simple data structure can be used to implement lots of interesting things like:

- text-decorations, colors, fonts etc in a word processor (e.g. set a range of the string to have the "bold" attribute)
- semantic text analysis systems (with attributes like "verb" and "noun")
- core text extraction

Examples
--------

[](#examples)

```
  use apemsel\AttributedString\AttributedString;

  // ...

  $as = new AttributedString("The quick brown fox");

  $as->setLength(10, 5, "color"); // "brown" has attribute "color"
  $as->is("color", 12); // == true
  $as->toHtml(); // "The quick brown fox"

  $as->setPattern("/[aeiou]/", "vowel"); // vowels have attribute "vowel"
  $as->getAttributes(12); // char at offset 12 has attributes ["color", "vowel"]

  $as->combineAttributes("and", "color", "vowel", "colored-vowel"); // also use "or", "not", "xor" to combine attributes
  $as->is("colored-vowel", 12); // "o" of "brown" is a color vowel ;-)

  $as->setSubstring("fox", "noun"); // all instances of "fox" have attribute "noun"
  $as->is("noun", 16); // true, char at offset 16 is part of a noun

  $as->searchAttribute("vowel"); // 2, first vowel starts at offset 2
  $as->searchAttribute("vowel", 0, true); // [2, 1], first vowel starting at offset 0 is at offset 2 with length 1

  // MutableAttributedString can be modified after creation and tries to be smart about the attributes
  $mas = new MutableAttributedString("The brown fox");
  $mas->setLength(0, 13, "bold");
  $mas->insert(4, "quick "); // "The quick brown fox";
  $mas->is("bold", 6) // true, "quick" is now also bold since the inserted text was inside the "bold" attribute
  $mas->delete(10, 6) // "The quick fox"

  // TokenizedAttributedString tokenizes the given string, can set attributes by token
  // and maintains the tokens' offsets in the original string.
  $tas = new TokenizedAttributedString("The quick brown fox"); // tokenize using the default whitespace tokenizer
  $tas->getToken(2); // "brown"
  $tas->setTokenAttribute(2, "bold"); // "brown" is "bold"
  $tas->getTokenOffset(2); // 10, "brown" starts at offset 10
  $tas->getTokenOffsets(); // [0, 4, 10, 16], start offsets of the tokens in the string
  $tas->setTokenRangeAttribute(2, 3, "underlined"); // set tokens 2 to 3 to "underlined"
  $tas->getAttributesAtToken(2); // ["bold", "underlined"]
  $tas->lowercaseTokens(); // convert tokens to lowercase
  $tas->setTokenDictionaryAttribute(["a", "an", "the"], "article"); // set all tokens contained in given dictionary to an attribute
  $tas->getAttributesAtToken(0); // "article"
```

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

[](#installation)

### Using Composer (recommended)

[](#using-composer-recommended)

```
composer require apemsel/AttributedString

```

Documentation
-------------

[](#documentation)

See the generated phpdoc API documentation in the doc/ directory or try

###  Health Score

43

—

FairBetter than 90% of packages

Maintenance50

Moderate activity, may be stable

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 99.4% 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

Every ~128 days

Recently: every ~735 days

Total

24

Last Release

823d ago

Major Versions

v0.7.0-beta → v1.0.02016-03-02

v1.5.3 → v2.0.02016-03-16

v2.0.0 → v3.0.02024-03-29

PHP version history (3 changes)v0.0.1-alphaPHP &gt;=5.4

v0.5.0-alphaPHP &gt;=5.5

v3.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/196454?v=4)[Adrian Pemsel](/maintainers/apemsel)[@apemsel](https://github.com/apemsel)

---

Top Contributors

[![apemsel](https://avatars.githubusercontent.com/u/196454?v=4)](https://github.com/apemsel "apemsel (165 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

stringlanguagetokentexttokenizeranalysisnlpattributemutablespanattributedtokenized

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/apemsel-attributed-string/health.svg)

```
[![Health](https://phpackages.com/badges/apemsel-attributed-string/health.svg)](https://phpackages.com/packages/apemsel-attributed-string)
```

###  Alternatives

[rajentrivedi/tokenizer-x

TokenizerX calculates required tokens for given prompt

91242.5k3](/packages/rajentrivedi-tokenizer-x)[creof/wkb-parser

Parser for well-known binary (WKB/EWKB) object data

605.3M16](/packages/creof-wkb-parser)[creof/geo-parser

Parser for geography coordinate strings

614.5M15](/packages/creof-geo-parser)[creof/wkt-parser

Parser for well-known text (WKT) object strings

545.0M16](/packages/creof-wkt-parser)[hybridlogic/classifier

A Naive Bayesian classification library for PHP with support for different tokenizers to optimize string classification.

801.4k](/packages/hybridlogic-classifier)[funivan/php-tokenizer

Wrapper around token\_get\_all. Easy to extract and modify php code.

1238.5k2](/packages/funivan-php-tokenizer)

PHPackages © 2026

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