PHPackages                             arnaudleroy-studio/mohitkhare - 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. arnaudleroy-studio/mohitkhare

ActiveLibrary

arnaudleroy-studio/mohitkhare
=============================

Developer utilities and token counting tools.

00PHP

Since Mar 28Pushed 1mo agoCompare

[ Source](https://github.com/arnaudleroy-studio/mohitkhare-php)[ Packagist](https://packagist.org/packages/arnaudleroy-studio/mohitkhare)[ RSS](/packages/arnaudleroy-studio-mohitkhare/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

MohitKhare PHP Utilities
========================

[](#mohitkhare-php-utilities)

[![Packagist Version](https://camo.githubusercontent.com/4407e1aceeb342e6da0bf77b8b91d9b049463be5404bbc32a77acef84ce57025/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61726e6175646c65726f792d73747564696f2f6d6f6869746b68617265)](https://packagist.org/packages/arnaudleroy-studio/mohitkhare)[![PHP Version](https://camo.githubusercontent.com/2b95aac0627fee5b1e49c61cbaf1ccde7001c4f110c10c6302f0b3d46359a99a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f61726e6175646c65726f792d73747564696f2f6d6f6869746b68617265)](https://packagist.org/packages/arnaudleroy-studio/mohitkhare)[![License](https://camo.githubusercontent.com/7d60622d9e40336dbc46be8f0046ca4ff5769a34116d3693b15dc0812aed2788/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f61726e6175646c65726f792d73747564696f2f6d6f6869746b68617265)](LICENSE)

A collection of developer utilities extracted from production projects at mohitkhare.me, focused on token estimation, string analysis, and text processing for PHP applications that interface with large language models. Requires PHP 8.0+ and uses strict types, enums, and readonly properties where applicable.

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

[](#installation)

```
composer require arnaudleroy-studio/mohitkhare
```

Quick Start
-----------

[](#quick-start)

### Estimate token counts

[](#estimate-token-counts)

```
use MohitKhare\TokenEstimator;

$estimator = new TokenEstimator();

// Quick estimate for a single string
$count = $estimator->estimate('The quick brown fox jumps over the lazy dog.');
echo "{$count} tokens"; // ~10 tokens

// Batch estimation with named arguments
$results = $estimator->estimateBatch(
    texts: ['Hello world', 'PHP is a general-purpose scripting language', $longArticle],
    model: 'gpt-4',
);
```

### Analyze text properties

[](#analyze-text-properties)

```
use MohitKhare\TextAnalyzer;

$analyzer = new TextAnalyzer();

$stats = $analyzer->analyze('Your input text goes here...');

// Array destructuring for readability
['words' => $words, 'sentences' => $sentences, 'readability' => $score] = $stats;

echo "Flesch-Kincaid readability: {$score}";
```

### Truncate to token budgets

[](#truncate-to-token-budgets)

```
// Fit a prompt within model context limits
$truncated = $estimator->truncate(
    text: $longDocument,
    maxTokens: 4096,
    strategy: 'end',
);

// Null-safe access when source might be empty
$preview = $estimator->truncate(
    text: $input?->getContent() ?? '',
    maxTokens: 200,
)?->getText();
```

### Chain operations with arrow functions

[](#chain-operations-with-arrow-functions)

```
$articles = ['First article body...', 'Second article body...', 'Third...'];

// Map to token counts in one pass
$counts = array_map(fn(string $text) => $estimator->estimate($text), $articles);

// Filter articles that fit within budget
$withinBudget = array_filter(
    $articles,
    fn(string $text) => $estimator->estimate($text)
