Search by

zdenekgebauer / lang-nbsp

zdenekgebauer

Inserts non-breaking spaces into text and HTML according to typographic rules of the given language

Package info

gitlab.com/zdenekgebauer/langnbsp

Issues

pkg:composer/zdenekgebauer/lang-nbsp

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

dev-master 2026-11-29 18:52 UTC

This package is auto-updated.

Last update: 2026-11-29 18:05:03 UTC


README

Inserts non-breaking spaces into text and HTML according to typographic rules of the given language.

Supported locales: Locale::CS, Locale::EN, Locale::PL, Locale::SK.

Requirements

PHP 8.4 or newer with ext-dom, ext-libxml, ext-mbstring and ext-pcre. No other runtime dependencies.

Installation

composer require zdenekgebauer/lang-nbsp

Add non-breaking spaces

For plaintext:

use ZdenekGebauer\LangNbsp\Fixer;
use ZdenekGebauer\LangNbsp\Locale;
use ZdenekGebauer\LangNbsp\ReplaceMode;

$text = Fixer::addNbsp($czechText, Locale::CS);
$text = Fixer::addNbsp($englishText, Locale::EN);
$text = Fixer::addNbsp($polishText, Locale::PL);
$text = Fixer::addNbsp($slovakText, Locale::SK);

// language code coming from a config or a request
$text = Fixer::addNbsp($anyText, Locale::from($code));

addNbsp() is meant for short texts — headlines, labels, table cells, teasers — where line breaks are not rendered anyway. It normalises the whitespace before applying any rule: the text is trimmed and every run of whitespace, line breaks included, collapses into a single space. Do not run it over a multi-paragraph document, the paragraphs would end up on a single line.

For HTML:

$html = Fixer::addNbspHtml($czechTextWithHtml, Locale::CS);
$html = Fixer::addNbspHtml($englishTextWithHtml, Locale::EN);

addNbspHtml() has no such limit — it normalises each text node separately, which is what a browser does with whitespace anyway. The content of <code>, <pre>, <script>, <style> and <textarea> is left alone, and so are tag attributes.

How much to replace

ReplaceMode is a scale, not a set of independent switches — each step binds more words than the previous one:

// narrow column: bind as little as possible, so that the text does not overflow the column
$text = Fixer::addNbsp($czechText, Locale::CS, ReplaceMode::MINIMAL);
// default
$text = Fixer::addNbsp($czechText, Locale::CS, ReplaceMode::STANDARD);
// wide text: bind by every supported rule
$text = Fixer::addNbsp($czechText, Locale::CS, ReplaceMode::ALL);

Every bound pair is a place where the line cannot break, so in a narrow column too many of them push words past the column width or tear justified lines apart. The wider the text, the more can be bound safely.

modecs, sken, pl
MINIMALone-letter prepositions and conjunctionsall rules
STANDARD (default)the above + §, † and short words (aby, do, na, tzv. …)all rules
ALLthe above + units after a number, dates, academic titles, more abbreviationsall rules

The gradation only makes sense where there is enough to grade. Czech and Slovak have rules in several layers, so the mode picks how deep to go. English and Polish rules are a single short list — FixerEn and FixerPl therefore ignore the mode and always apply everything. Passing MINIMAL to them is not an error, it simply changes nothing.

Known issues and pitfalls

  • invalid html code (eg code with some unclosed tags) is repaired by the HTML5 parser
  • named html entities on input (&iacute;) are decoded to UTF-8 characters on output (í)
  • addNbsp() returns the U+00A0 character (Fixer::NBSP), not the &nbsp; entity — the input is plain text, so the output is plain text too. addNbspHtml() returns &nbsp;, because the HTML serializer writes the character that way
  • text with many hard spaces can be wider than a dedicated narrow column and cause problems with design
  • text with hard spaces stored in a database can complicate searching
  • English rules are only basic
  • Polish rules cover only one-letter prepositions and conjunctions (a i o u w z)

Changelog

See CHANGELOG.md.

License

Released under the MIT license, see LICENSE.