php-io-extensions / unibreak
1:1 PHP bindings of libunibreak: Unicode line, word and grapheme breaking
Package info
github.com/php-io-extensions/unibreak
Type:php-ext
Ext name:ext-unibreak
pkg:composer/php-io-extensions/unibreak
Requires
- php: >=8.4
Requires (Dev)
- ext-posi: *
- pestphp/pest: ^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-05 05:41:35 UTC
README
ext-unibreak
libunibreak bound 1:1 into PHP 8.4+, at the 5.1 API
Text that is wider than its box has to wrap, and where it may wrap is not "at the spaces". A line may end after a hyphen but not before a closing quote, never inside "1,000", anywhere between two ideographs, and not before "ぁ" when Japanese is set strictly. A cursor that moves by one character has to step over "é" written as e and a combining accent, and over 🇺🇸, which is two code points. Splitting on spaces, or on bytes, gets every one of those wrong.
libunibreak answers those questions with Unicode's own rules: the line breaking algorithm of UAX #14, and the word and grapheme cluster boundaries of UAX #29. Hand it a whole text and it writes one byte for each code unit into a buffer you give it, saying whether a line, a word or a grapheme may end there. It is written in C, small, and found on every Linux distribution and in Homebrew.
The only problem? PHP can't reach it. This extension binds libunibreak one to one under the C names, at the libunibreak 5.1 API: line, word and grapheme breaking of UTF-8, UTF-16 and UTF-32 text, whether a line may break between two characters, and reading a text one code point at a time. The breaks are written to the address you pass, as in C; the extension allocates nothing. No defaults, no composites, no opinions: what libunibreak documents is what you call.
Basic usage
Most ways you will interface with ext-unibreak are in two parts -
Find where a line may end. set_linebreaks_utf8() writes one LINEBREAK_* byte for each byte of the text to the address $brks, which must hold that many bytes. The memory is yours to give: here ext-posi allocates it and reads it back.
$text = "Hello, wörld!\nNext"; $brks = posi_mem_alloc(strlen($text)); set_linebreaks_utf8($text, strlen($text), 'en', $brks); $breaks = unpack('C*', posi_mem_read($brks, strlen($text), 0)); foreach ($breaks as $at => $break) { if ($break === LINEBREAK_ALLOWBREAK || $break === LINEBREAK_MUSTBREAK) { printf("%s after byte %d: %s\n", $break === LINEBREAK_MUSTBREAK ? 'must' : 'may ', $at - 1, json_encode(substr($text, 0, $at), JSON_UNESCAPED_UNICODE)); } } posi_mem_free($brks);
Step through it by code point. ub_get_next_char_utf8() answers the code point at $ip and moves $ip past it, and answers EOS at the end:
$ip = 0; while (($ch = ub_get_next_char_utf8($text, strlen($text), $ip)) !== EOS) { printf("U+%04X ", $ch); }
$ php wrap.php may after byte 6: "Hello, " must after byte 14: "Hello, wörld!\n" U+0048 U+0065 U+006C U+006C U+006F U+002C U+0020 U+0077 U+00F6 U+0072 U+006C U+0064 U+0021 U+000A U+004E U+0065 U+0078 U+0074
The last byte of a text that does not end in a line break is LINEBREAK_INDETERMINATE: libunibreak cannot tell what follows.
Requirements
| OS | Linux or macOS. composer.json lists those two families. |
| PHP | 8.4 or newer, NTS or ZTS. |
| Libraries | libunibreak 5.1 or newer, with its headers, found through pkg-config (libunibreak). 5.1 is the floor: nothing newer is bound. |
To build it you need a C compiler, pkg-config, libunibreak's development files, and the development files of the PHP you intend to load it into:
# Debian, Ubuntu, Raspberry Pi OS sudo apt install build-essential pkg-config libunibreak-dev php8.4-dev # macOS: Xcode Command Line Tools, and Homebrew's PHP ships phpize and php-config xcode-select --install brew install pkgconf libunibreak php@8.4
Installation
With PHP PIE (recommended)
PIE is the PHP Foundation's installer for extensions. It downloads the tagged source from Packagist, builds it against the PHP on your PATH, installs the module and enables it. sudo where your PHP's extension directory is root-owned, as on Debian; not on a Homebrew Mac. Install libunibreak first:
pie install php-io-extensions/unibreak
Manually (install script)
The repository ships two installers, one per platform. Each one builds for each PHP it targets, installs unibreak.so into that PHP's extension_dir, writes 30-unibreak.ini into its conf.d scan directory, and checks the module loads. The macOS one builds in a temporary copy of the checkout and ad-hoc signs the module so the system loads it on first use; the Debian one builds in the checkout, writes the ini into the CLI, FPM and Apache conf.d of that PHP, and cleans the build products out afterwards.
git clone https://github.com/php-io-extensions/unibreak.git cd unibreak ./install-debian-trixie.sh # Debian Trixie, Ubuntu 24.04+, Raspberry Pi OS: the php on PATH ./install-macos.sh # Homebrew php@8.4 and php@8.4-zts ./install-macos.sh /path/to/bin/php # the macOS script takes specific PHP binaries
Manually (phpize)
git clone https://github.com/php-io-extensions/unibreak.git cd unibreak phpize ./configure --enable-unibreak make -j"$(nproc 2>/dev/null || sysctl -n hw.ncpu)" sudo make install
Then enable it: add extension=unibreak.so to your php.ini, or drop that line into a file in the scan directory that php --ini reports.
Verify
php --ri unibreak also names the libunibreak headers it was built against and the library it runs with:
$ php --ri unibreak unibreak unibreak support => enabled Version => 0.11.0 libunibreak headers => 8.0 libunibreak library => 8.0
API
Function and constant names are the C names, so libunibreak's headers, whose comments document each call, are the reference: 18 functions, one stub per header in stubs/, each declared with its PHP types. SUPPORT.md lists every function libunibreak 5.1 declares in the headers it installs, bound or not; the API badge is computed from it.
| Stub | Functions | What it covers |
|---|---|---|
linebreak |
7 | init_linebreak(), set_linebreaks_utf8(), set_linebreaks_utf16(), set_linebreaks_utf32(), set_linebreaks_utf8_per_code_point(), set_linebreaks_utf16_per_code_point(), is_line_breakable(); the LINEBREAK_* values. |
wordbreak |
4 | init_wordbreak(), set_wordbreaks_utf8(), set_wordbreaks_utf16(), set_wordbreaks_utf32(); the WORDBREAK_* values. |
graphemebreak |
4 | init_graphemebreak(), set_graphemebreaks_utf8(), set_graphemebreaks_utf16(), set_graphemebreaks_utf32(); the GRAPHEMEBREAK_* values. |
unibreakdef |
3 | ub_get_next_char_utf8(), ub_get_next_char_utf16(), ub_get_next_char_utf32(); EOS. |
unibreak |
— | UNIBREAK_VERSION, the version of the headers the extension was built against. |
Translations
Every C parameter is its own PHP parameter, in C order. The only differences from C:
const utf8_t *,const utf16_t *andconst utf32_t *text is astringof the code units in the machine's byte order, read where PHP keeps it.lencounts code units: bytes for UTF-8, pairs of bytes for UTF-16, groups of four for UTF-32.char *brksis anint: the address libunibreak writes to. Theset_*breaks_*calls writelenbytes there, and the_per_code_pointcalls one byte a code point. The address is trusted. With alenof 0 nothing is written, and the address is not read.const char *langis a?string;nullis NULL, no language. libunibreak reads up to the first NUL byte.size_t *ipis a by-referenceint, read before the call and written after it.utf32_tandsize_tareint;EOSis 4294967295.- The
init_*calls do nothing in libunibreak 5.1 ("kept for binary compatibility"), and are bound because libunibreak still declares them.
Refusals
The extension refuses two things, each of which reads outside the text otherwise (tests/Probes shows each crash on a probe build):
- A
lenthat is negative or runs past the string, in every call that takes a text:ValueError. libunibreak readslencode units without a bound of its own. - An
$ipoutside 0 toleninub_get_next_char_*():ValueError. libunibreak asserts*ip <= len, which aborts, and where the assert is compiled out reads the text at*ip.
Extensions you might use with this one
- ext-harfbuzz: shapes the text of each line once you know where the lines end.
- ext-posi: native memory by address, for the breaks to be written to.
AI-assisted development
This codebase was built with LLM assistance under human direction. Prompts, reviews, and architectural decisions were owned by a professional software engineer — the model accelerates drafting and exploration; it does not replace judgment on APIs, dependency direction, or what ships.
Agentic Documentation
This repository carries an Open Knowledge Format bundle at .okf/: Markdown with YAML frontmatter, readable by you and loadable by a coding agent. It records what the README doesn't: every function and constant by stub, how the text, the lengths and the break address reach libunibreak, which arguments pass through and which are refused, how to build release and probe builds, how to add a binding, and a log of every change that altered behavior.
Start at .okf/index.md. AGENTS.md is the short rule sheet an agent reads before touching the C, the stubs, or packaging; each rule points into the bundle for the reason behind it.
Point your agent at the repo and it has the same context the maintainer has. Point yourself at it when the README's answer is "it does X" and you want "because".
Resources
- libunibreak: the library and its headers, whose comments document each call; this README only adds what PHP changes.
- libunibreak 5.1: the upstream tag the stubs and SUPPORT.md are checked against.
- UAX #14 and UAX #29: the line breaking algorithm and the text segmentation rules libunibreak implements.
- PIE: the PHP Foundation's extension installer.
gen_stub.php: the php-src stub format.stubs/holds the API declarations; each*_arginfo.his generated from its stub.- Open Knowledge Format: the spec the
.okf/bundle follows.
Running the test suite
The suite is Pest v4: line, word and grapheme breaks of UTF-8, UTF-16 and UTF-32 text, the language a line break is read in, stepping through a text by code point, the lengths and indexes that are refused, and the coverage script. The break tests write to memory ext-posi allocates, and skip where it is not loaded. Build the module, then enable it through an ini scan directory rather than -d, so the child processes the tests start load it too:
git clone https://github.com/php-io-extensions/unibreak.git cd unibreak phpize && ./configure --enable-unibreak && make -j"$(nproc 2>/dev/null || sysctl -n hw.ncpu)" composer install mkdir -p /tmp/unibreak-ini && echo "extension=$PWD/modules/unibreak.so" > /tmp/unibreak-ini/unibreak.ini PHP_INI_SCAN_DIR=":/tmp/unibreak-ini" php -d memory_limit=128M vendor/bin/pest
Tests: 4 skipped, 41 passed (71 assertions) Duration: 0.08s
The four skipped tests are in tests/Probes: they run only against a probe build (./configure --enable-unibreak --enable-unibreak-probes, or UNIBREAK_CONFIGURE=--enable-unibreak-probes ./install-macos.sh), which can turn each refusal off to show the crash it prevents. Never install a probe build for use.
The leading : keeps your PHP's own scan directory and adds the second one. If that PHP already loads an installed unibreak.so, the suite warns Module "unibreak" is already loaded and tests the installed one instead; copy the scan directory somewhere, replace its unibreak ini with the line above, and point PHP_INI_SCAN_DIR at the copy without the colon. A module only loads in the PHP whose phpize built it: for Homebrew's ZTS build, run phpize and pass --with-php-config from /opt/homebrew/opt/php@8.4-zts/bin. Set XDEBUG_MODE=off for any timing you intend to read.
Contributing
Open an issue first for anything beyond a typo. Pull requests change a stub in stubs/ and the C under src/ together, regenerate the *_arginfo.h files with gen_stub, regenerate SUPPORT.md and the API badge with php scripts/support.php, and add a Pest test for the behavior. A new refusal needs a probe test that shows the crash it prevents. CI must be green on every cell of the matrix, NTS and ZTS alike. Read AGENTS.md for the rules the code follows.
Security
ext-unibreak hands text and an address to libunibreak, which reads the one and writes to the other. See SECURITY.md for what it guards against and how to report a vulnerability.
License
MIT. See LICENSE. libunibreak is under the zlib license; it is linked, not shipped.