Skip to content

Latest commit

 

History

History
86 lines (64 loc) · 4.12 KB

slugify_transformer.md

File metadata and controls

86 lines (64 loc) · 4.12 KB

SlugifyTransformer

Convert a string into a slug. The value is transliterated with a \Transliterator (by default, into ASCII latin letters), HTML tags are stripped if strip_tags is true, the result is trimmed and lowercased (multibyte characters included), every sequence of characters matching replace is replaced by separator, and leading and trailing separators are removed.

Requires the intl PHP extension (suggested by the bundle): without it, the transformer fails with an explicit message.

Transformer reference

  • Service: CleverAge\ProcessBundle\Transformer\String\SlugifyTransformer
  • Transformer code: slugify

Accepted inputs

string, or a scalar or a \Stringable cast to a string. null gives null. An array or another object (a \BackedEnum included) throws an UnexpectedValueException.

Possible outputs

  • string
  • null if the input is null

Options

Code Type Required Default Description
transliterator string 'Any-Latin; Latin-ASCII' Transliterator identifier, passed to \Transliterator::create() (see Notes)
replace string '/[^a-z0-9]+/' Regular expression of the characters to replace (applied on the lowercased string), checked with the configuration
separator string '_' Replacement string, also trimmed from both ends of the result
strip_tags bool false Strip the HTML tags with strip_tags(), for an HTML text ('<b>Foo</b>' becomes 'foo' instead of 'b_foo_b'). It removes everything after a < that does not open a tag ('Tom<Jerry' becomes 'tom', '<3 love' becomes '')

Examples

  • 'Hélène Dupont' becomes 'helene_dupont', 'Cœur' 'coeur', 'Привет мир' 'privet_mir'
# Transformer options level
slugify: ~
  • Use a dash as separator, and strip the tags of an HTML text: '<p>Hélène Dupont</p>' becomes 'helene-dupont'
# Transformer options level
slugify:
  separator: '-'
  strip_tags: true
  • Slugs of 5.x (only the accents are removed, HTML tags are stripped): 'Cœur <b>Straße</b>' becomes 'c_ur_stra_e'
# Transformer options level
slugify:
  transliterator: 'NFD; [:Nonspacing Mark:] Remove; NFC'
  strip_tags: true

Notes

The default transliterator converts the other scripts into latin letters, then the latin letters into ASCII ('Cœur' becomes 'coeur', 'Straße' 'strasse', 'Łódź' 'lodz'). The scripts it converts depend on the ICU data installed with the intl extension: the reduced data of some images (e.g. the icu-data-en package of Alpine) only convert the cyrillic and greek scripts, a chinese, japanese, korean or arabic text is left unchanged and gives '' with the default replace pattern. Install the full ICU data (icu-data-full on Alpine) to convert them ('日本語' becomes 'ri_ben_yu').

In 5.x, the default transliterator was 'NFD; [:Nonspacing Mark:] Remove; NFC', which only removes the accents (the combining marks): the letters that are not decomposed into a base letter and an accent are then removed by the default replace pattern ('Cœur' becomes 'c_ur'), and a text without latin letters gives ''.

An invalid transliterator identifier (rejected by \Transliterator::create()), or an option that is not a string, raises an InvalidOptionsException when the options are resolved, i.e. when the transformer is configured, not on the first transformed value.

A value that cannot be transliterated (e.g. invalid UTF-8) throws an UnexpectedValueException.