script-share is a PHP library for anyone who needs to check which writing system a piece of text is in: Devanagari, Bengali, Tamil, Telugu, Latin and 19 more. It uses no model and makes no network calls. The code is at https://github.com/IndianOpinion/script-share, under the MIT licence. Version v0.1.0 was released on 14 November 2026.
A pipeline asks a language model to write in English, Hindi or Tamil. Sometimes the model answers in another language, usually because the prompt says “write in the language of the source” and the source was Telugu or Kannada. The reply is fluent, well formed and wrong, and nothing downstream notices. The same failure appears as a translated headline over an untranslated body, and as one Indian script bleeding into text meant for another.
The cheap test is the writing system, since each script has its own Unicode script property. The naive version is easy to get wrong. The naive version fails in three ways:
false on invalid UTF-8, which cast to a number is zero letters in every script. A broken reply was reported as English and passed.letters() + ignored() === codePoints() always true.A code point counts when it is a letter, or in the default mode a combining mark, and its Unicode Script property is a real script. Digits, punctuation (including the danda), symbols, emoji and whitespace never count. Inherited marks count for the script of the code point before them.
One compiled PCRE pattern tokenises the text, with one alternative per script, so each code point is read once and nothing is rescanned. Text is processed in 16 KiB windows cut after whitespace, which keeps memory flat. The main files are Accumulator.php (buffering, windows, UTF-8 validation), Internal/Scanner.php (counting), Internal/Pattern.php (the pattern), Result.php, Check.php and LanguageMap.php (language tag to expected scripts).
Script data comes from the PCRE library PHP was built with. On PCRE2 10.40 and later the pattern asks for the Script property explicitly with \p{sc:Devanagari}, because from 10.40 a bare script name means Script_Extensions. A golden corpus of 308 lines checks that results match across builds.
use IndianOpinion\ScriptShare\Script;
use IndianOpinion\ScriptShare\ScriptShare;
use IndianOpinion\ScriptShare\Verdict;
$check = ScriptShare::checkLanguage($reply, 'hi', 0.80);
if ($check->verdict() !== Verdict::Pass) {
error_log($check->reason());
}
ScriptShare::isMostly($body, [Script::Deva, Script::Latn], 0.95);
In the repo’s own benchmark on synthetic text (PHP 8.3, PCRE2 10.42), 1 MiB of mixed Hindi and English takes about 70 ms with PCRE JIT on and under 0.4 MiB of extra memory. With JIT off it takes about 200 ms.
InvalidUtf8Exception with the byte offset, or in substitute mode counts each invalid subpart as one ignored code point.RegexException. It is never reported as zero letters.InvalidArgumentException. They are never clamped.check() has no default minShare, on purpose: every caller’s tolerance for quoted English differs.skipLinks on by default. The PRD fixes it as off, and on by default also dropped Hindi hashtags (one test string gave 5 Devanagari letters instead of 9). The default is back to off.Zzzz (Armenian, Georgian and so on).Test status: this is v0.1.0. 198 PHPUnit tests (3,454 assertions) pass, with PHPStan level 8 and phpcs clean, on fixed corpora and stand-in inputs. It has not run on a live site. Only PHP 8.3 was run. PHP 8.1 and 8.2 are covered by a CI workflow that is parked on a separate branch, so CI has not yet run on GitHub.
Requires PHP 8.1 or newer with ext-mbstring and PCRE with Unicode support. No ext-intl, no runtime dependencies.
The package is not on Packagist yet, so first add a Composer vcs repository entry for https://github.com/IndianOpinion/script-share.
composer require indianopinion/script-share
Without Composer, copy the repository (or just src/) and require src/bootstrap.php.