Text
Text is a string: anything in quotes. 'Single quotes' keep what you write exactly as it is, and "double quotes" fill in what is in { }, so "Hello { $name }" reads the variable. For text over several lines, write a text block between ``` lines, which keeps every line break as written.
Text methods never change the text they are called on; each gives new text (or a number, a list or a bool), so chain them or keep the result in a variable:
$name = ' ada LOVELACE '
$tidy = $name->trim()->title()
echo $tidy // 'Ada Lovelace' because the spaces go and each word gets a capital
echo $name // ' ada LOVELACE ' because the text itself is unchanged
Positions count from 0, and a negative position counts from the end. A character such as an emoji counts as two places, and no method cuts one in half. Comparisons match case exactly unless you pass the ignore_case flag.
Find out about text: count · count_of · is_empty · is_blank · is_numeric · contains · starts_with · ends_with · index_of · matches
Take part of it: first · last · slice · before · after · truncate · find_all
Tidy and change it: trim · squish · strip · replace · upper · lower · capitalize · title · case · pad · repeat · reverse · wrap · mask · normalize_unicode
Break it into a list: split · lines · blocks · words · chars · numbers · grid · shapes
Read it as another value: to_number · to_int · to_bool · to_date, or another format with ->convert()
Make it safe to send: escape · url_encode · url_decode · to_base64 · from_base64 · md5 · sha1 · sha256 · sha512 · hmac
->after()
->after(text, last?) gives the text after the first time text occurs, or after the last time with last. When text isn’t there, you get the whole text back:
$file = 'archive.tar.gz'
echo $file->after('.') // 'tar.gz' because it cuts at the first dot
echo $file->after('.', last) // 'gz' because it cuts at the last dot
echo 'no-at-sign'->after('@') // 'no-at-sign' because there is no @ to cut at
->before()
->before(text, last?) gives the text before the first time text occurs, or before the last time with last. When text isn’t there, you get the whole text back:
$email = 'joan@example.com'
echo $email->before('@') // 'joan'
echo 'archive.tar.gz'->before('.', last) // 'archive.tar' because it cuts at the last dot
->blocks()
->blocks() splits the text at blank lines, so each block is a group of lines. A single line break stays inside a block; only an empty line starts a new one. Each block is trimmed of the line breaks around it:
$pantry = ```text
tea
milk
flour
sugar
eggs
```
$shelves = $pantry->blocks()
echo $shelves->count() // 2 because one blank line splits the text in two
echo $shelves->map(.lines()->count()) // [2, 3] because the first block has 2 lines and the second 3
->capitalize()
->capitalize() makes the first letter upper case and leaves the rest as it is:
echo 'élan and style'->capitalize() // 'Élan and style' because only the first letter changes
echo 'iPhone'->capitalize() // 'IPhone'
->case()
->case(style) rewrites text as a name in one style: snake (user_id), kebab (user-id), camel (userId) or pascal (UserId). It finds the words in spaces, separators and changes of case alike, so you can go from any style to any other:
echo 'Order Total'->case(snake) // 'order_total'
echo 'order_total'->case(camel) // 'orderTotal'
echo 'orderTotal'->case(kebab) // 'order-total'
echo 'order-total'->case(pascal) // 'OrderTotal'
->chars()
->chars() gives the characters as a list, one text each. A character such as an emoji is one item, however it is stored:
echo 'tea'->chars() // ['t', 'e', 'a']
echo '👍🏽x'->chars()->count() // 2 because the thumb and its skin tone are one character
->contains()
->contains(text, ignore_case?) is true when the text holds text anywhere. With ignore_case, upper and lower case match:
echo 'Green tea'->contains('tea') // true
echo 'Green tea'->contains('TEA') // false because case matters
echo 'Green tea'->contains('TEA', ignore_case) // true
->count()
->count() gives how many characters the text has:
echo 'tea'->count() // 3
echo ''->count() // 0
->count_of()
->count_of(match) counts how many times a piece of text occurs. Occurrences don’t overlap:
echo 'banana'->count_of('an') // 2 because b-an-an-a has two
echo 'banana'->count_of('x') // 0
->ends_with()
->ends_with(text, ignore_case?) is true when the text ends with text:
echo 'photo.JPG'->ends_with('.jpg') // false because case matters
echo 'photo.JPG'->ends_with('.jpg', ignore_case) // true
->escape()
->escape() makes text safe to put in HTML: <, >, & and quotes become entities, so what a user typed shows as text rather than becoming markup:
echo '<b>Tea & milk</b>'->escape() // '<b>Tea & milk</b>'
Pages escape what they print already; this is for HTML you build yourself.
->find_all()
->find_all(pattern) gives every match of a regular expression, in order:
echo 'tea 12, milk 7'->find_all(/\d+/) // ['12', '7'] because those are the runs of digits
->numbers() gives the numbers themselves, as numbers.
->first()
->first() gives the first character, or '' for empty text:
echo 'tea'->first() // 't'
echo ''->first() // ''
->from_base64()
->from_base64() decodes base64 text back into the text it was made from. Text that isn’t base64 is an error:
echo 'dGVh'->from_base64() // 'tea'
->grid()
->grid() turns lines of characters into rows of single characters, so $grid[row][column] is one character. Row 0 is the top line and column 0 the first character. A line break at the end adds no empty row:
$garden = ```text
#.#
..#
```
$plot = $garden->grid()
echo $plot // [['#', '.', '#'], ['.', '.', '#']]
echo $plot[0][1] // '.' because row 0 is #.# and column 1 is its middle
echo $plot[1][2] // '#' because row 1 is ..# and column 2 is its last
To go through the rows or the columns, read the grid with ->cells(rows) or ->cells(columns).
->hmac()
->hmac(key, algorithm?) signs the text with a secret key and gives the signature in hex, SHA-256 unless you pass sha1 or sha512. It is how webhooks prove where a message came from. Compare signatures with @secure_equals(), which takes the same time however much of the two matches:
$body = '{"id": 1}'
$signature = $body->hmac('whsec_test')
echo $signature->count() // 64 because SHA-256 in hex is 64 characters
echo @secure_equals($signature, $body->hmac('whsec_test')) // true because the same key signs the same text the same way
->index_of()
->index_of(text, ignore_case?, last?) gives the position where text first occurs, or with last, where it last occurs. It gives -1 when text isn’t there:
echo 'banana'->index_of('an') // 1 because positions start at 0
echo 'banana'->index_of('an', last) // 3
echo 'banana'->index_of('x') // -1
->is_blank()
->is_blank() is true when the text is empty or has only spaces, tabs and line breaks. Use it to check what someone typed into a form:
echo ''->is_blank() // true
echo ' '->is_blank() // true because spaces alone count as blank
echo ' tea '->is_blank() // false
->is_empty()
->is_empty() is true when the text has no characters at all. Spaces count as characters, so ' ' isn’t empty; use ->is_blank() for that:
echo ''->is_empty() // true
echo ' '->is_empty() // false because it has three spaces
->is_numeric()
->is_numeric() is true when the whole text is a number, such as '42', '-3.5' or '1e3'. Spaces around it are allowed:
echo '42'->is_numeric() // true
echo ' -3.5 '->is_numeric() // true because the spaces around it are fine
echo '42 cats'->is_numeric() // false because cats isn't part of a number
->last()
->last() gives the last character, or '' for empty text:
echo 'tea'->last() // 'a'
->lines()
->lines() gives one text per line. A blank line gives an empty text, so you can find the breaks between groups:
$note = ```text
tea
milk
juice
```
echo $note->lines() // ['tea', 'milk', '', 'juice']
echo $note->lines()->count() // 4 because the blank line is a line too
->blocks() splits at the blank lines instead.
->lower()
->lower() gives the text in lower case:
echo 'Green TEA'->lower() // 'green tea'
->mask()
->mask(char?, keep_end?) hides the text behind a character, * unless you give another, keeping the last keep_end characters so the reader can recognise it:
echo '4242424242424242'->mask('*', keep_end: 4) // '************4242' because only the last 4 show
echo 'secret'->mask() // '******'
->matches()
->matches(pattern) is true when a regular expression finds a match in the text. Anchor it with ^ and $ to test the whole text:
echo 'Tea'->matches(/^[a-z]+$/i) // true because i ignores case
echo 'Tea2'->matches(/^[a-z]+$/i) // false because 2 isn't a letter
->md5()
->md5() gives the MD5 hash of the text in hex. It is quick and fine for a cache key or a checksum, but not for passwords or signatures:
echo 'tea'->md5() // '7239ea2b5dc943f61f3c0a0276c20974'
echo 'tea'->md5()->count() // 32 because MD5 in hex is 32 characters
->normalize_unicode()
->normalize_unicode(form?) makes text that looks the same compare the same. é can be stored as one character or as e plus an accent; NFC (the default) joins them, NFD splits them:
echo 'café'->normalize_unicode(NFD)->count() // 5 because NFD stores é as e and an accent
echo 'café'->normalize_unicode()->count() // 4
->numbers()
->numbers() gives every number written in the text, in order, as numbers. A minus counts as a sign when nothing is joined to its left, as in x=-3; between two numbers, as in 1-3, it is a dash:
echo 'move 3 boxes from -1 to 12'->numbers() // [3, -1, 12] because -1 has a space before its minus
echo 'pages 4-9'->numbers() // [4, 9] because the minus joins 4 and 9, so it is a dash
echo 'price 2.50, qty 4'->numbers() // [2.5, 4]
->pad()
->pad(width, char?, start?) adds a character, a space unless you give another, until the text is width characters long. It pads at the end, or at the start with start. Text already that long is left alone:
echo '7'->pad(3, '0', start) // '007' because two zeros go in front
echo 'tea'->pad(5, '.') // 'tea..'
echo 'teapot'->pad(3) // 'teapot' because it is already longer than 3
->repeat()
->repeat(count) gives the text count times over:
echo 'ha'->repeat(3) // 'hahaha'
echo '-'->repeat(10) // '----------'
->replace()
->replace(old, new, ignore_case?) replaces every old with new. old can be a regular expression, and then new can use what its groups matched as $1 or ${name}:
echo 'banana'->replace('a', 'o') // 'bonono' because every a changes
echo 'Tea'->replace('t', 'p', ignore_case) // 'pea' because T matches t
echo 'too many spaces'->replace(/\s+/, ' ') // 'too many spaces'
echo '2026-12-01'->replace(/(\d+)-(\d+)-(\d+)/, '$3/$2/$1') // '01/12/2026' because the groups swap places
->reverse()
->reverse() gives the characters in the opposite order, keeping each character whole:
echo 'stressed'->reverse() // 'desserts'
->sha1()
->sha1() gives the SHA-1 hash of the text in hex, 40 characters. Prefer ->sha256() for anything new:
echo 'tea'->sha1()->count() // 40
->sha256()
->sha256() gives the SHA-256 hash of the text in hex, 64 characters. The same text always gives the same hash, and any change gives a different one:
echo 'tea'->sha256()->count() // 64
echo 'tea'->sha256() == 'tea'->sha256() // true because the same text gives the same hash
echo 'tea'->sha256() == 'Tea'->sha256() // false because one letter changed
->sha512()
->sha512() gives the SHA-512 hash of the text in hex, 128 characters:
echo 'tea'->sha512()->count() // 128
->shapes()
->shapes(mask?, square?, rectangle?, line?, circle?, of?, rotated?, size?, radius?, distance?) finds shapes drawn in text. Each line is a row and each character a cell, so the text doesn’t need ->grid() first, and a blank line starts the next layer of a 3D input. It works as the list method does: name a mask first (a type that extends Mask, or a list of them), or one of the built-in shapes with of: to say which cells make it. Each match comes back as { kind, points }, and each point says its row, column, value and the mask’s symbol drawn over it. See list ->shapes() for every argument.
$orchard = ```text
#.#.#
.#...
#.#.#
```
type Tree extends Mask {
layout: ```text
#.#
.#.
```
symbols: { '.': any }
}
echo $orchard->shapes(Tree)->count() // 1 because the V of three trees fits only at the top left
echo $orchard->shapes(Tree, rotated)->count() // 4 because turned upside down and on its sides it fits three more places
echo $orchard->shapes(Tree)->first().points->map(.column) // [0, 2, 1] because the points come in reading order: the top two, then the middle
->slice()
->slice(start, end?) gives the characters from position start up to, but not including, end, or to the end of the text. Negative positions count from the end:
$word = 'teapot'
echo $word->slice(0, 3) // 'tea' because it stops before position 3
echo $word->slice(3) // 'pot'
echo $word->slice(-3) // 'pot' because -3 is three from the end
->split()
->split(separator?) cuts the text at each separator and gives the parts as a list. Without a separator it cuts at spaces. The separator can be a regular expression:
echo 'tea,milk,juice'->split(',') // ['tea', 'milk', 'juice']
echo 'tea milk'->split() // ['tea', 'milk']
echo 'a, b,c'->split(/\s*,\s*/) // ['a', 'b', 'c'] because the spaces around each comma go too
->squish()
->squish() trims the text and makes every run of spaces, tabs and line breaks inside it one space:
echo ' too many spaces '->squish() // 'too many spaces'
->starts_with()
->starts_with(text, ignore_case?) is true when the text begins with text:
echo 'https://example.com'->starts_with('https://') // true
echo 'HTTP://x'->starts_with('http', ignore_case) // true because case is ignored
->strip()
->strip(prefix?, suffix?) takes off one exact prefix, suffix or both, where they are there. Unlike ->trim() it removes the whole piece once, not each character:
echo 'v1.2'->strip(prefix: 'v') // '1.2'
echo 'notes.txt'->strip(suffix: '.txt') // 'notes'
echo '[tea]'->strip(prefix: '[', suffix: ']') // 'tea'
echo 'tea'->strip(prefix: 'x') // 'tea' because there is no x to take off
->title()
->title() gives the text in Title Case: the first letter of each word upper case:
echo 'the tea party'->title() // 'The Tea Party'
->to_base64()
->to_base64(url?) encodes text as base64, which uses only letters, digits, +, / and =, so it can travel where other characters can’t, such as an HTTP header. With url, it uses - and _ instead and leaves off the = padding, so it fits in a URL. The result is a base64 value, whose ->decode() gives the text back:
echo 'tea'->to_base64() // 'dGVh'
echo 'tea?'->to_base64() // 'dGVhPw=='
echo 'tea?'->to_base64(url) // 'dGVhPw' because url leaves off the padding
echo 'tea'->to_base64()->decode() // 'tea' because the base64 value decodes back to the text
->to_bool()
->to_bool() reads text such as 'true', 'false', 'yes', 'no', '1' or '0' as a bool. Any other text is a Type error:
echo 'yes'->to_bool() // true
echo '0'->to_bool() // false
->to_date()
->to_date(format?) reads text as a date. Dates written the ISO way, such as '2026-11-22' or '2026-11-22 14:30', need no format; for any other way, give the pattern it is written in:
$day = '2026-11-22'->to_date()
echo $day.year // 2026
echo '22-11-2026'->to_date(dd-MM-yyyy).month // 11 because the pattern says the month is in the middle
->to_int()
->to_int(default?) reads text as a whole number, dropping anything after the decimal point. When the text isn’t a number, you get default; without one, that is a Type error, so pass a default for text you aren’t sure of:
echo '42'->to_int() // 42
echo '3.7'->to_int() // 3 because the .7 is dropped
echo 'abc'->to_int(-1) // -1 because abc isn't a number
->to_number()
->to_number(default?) reads text as a number, decimals and all. When the text isn’t a number, you get default; without one, that is a Type error that says so:
echo '3.7'->to_number() // 3.7
echo 'x'->to_number(0) // 0 because x isn't a number, so the default is used
echo 'x'->to_number(null) // null
->trim()
->trim(characters?, start?, end?) takes spaces, tabs and line breaks off both ends, or the characters you give. With start or end it trims only that end:
echo ' tea '->trim() // 'tea'
echo '//path//'->trim('/') // 'path'
echo ' tea '->trim(start) // 'tea ' because only the start is trimmed
->truncate()
->truncate(length, suffix?) keeps the first length characters and adds a suffix, … unless you give another, when there was more. Text that already fits is left alone:
echo 'Hello there, friend'->truncate(10) // 'Hello ther…'
echo 'Hello there, friend'->truncate(10, '...') // 'Hello ther...'
echo 'Hi'->truncate(10) // 'Hi' because it already fits
->upper()
->upper() gives the text in upper case:
echo 'Green tea'->upper() // 'GREEN TEA'
->url_decode()
->url_decode() turns URL escapes such as %20 back into the characters they stand for:
echo 'tea%20%26%20milk'->url_decode() // 'tea & milk'
->url_encode()
->url_encode() escapes text so it is safe inside a URL, such as a search term in a query string:
echo 'tea & milk'->url_encode() // 'tea%20%26%20milk' because spaces and & have a meaning in URLs
->words()
->words() gives the words in the text, without the spaces and punctuation between them. Letters in any script count, and don't or well-known stay one word:
echo 'Hello, world!'->words() // ['Hello', 'world']
echo "Don't stop"->words()->count() // 2 because don't is one word
->wrap()
->wrap(width) puts line breaks between words so that no line is longer than width, unless a single word is. It suits text for a terminal, such as one @terminal_width() wide:
$lines = 'Ann went home early'->wrap(width: 9)->lines()
echo $lines // ['Ann went', 'home', 'early'] because home early would be 10 characters