<let name="name" value="' ada LOVELACE '" />
<let name="tidy" value="$name->trim()->title()" />
<echo value="$tidy" /> <!-- 'Ada Lovelace' because the spaces go and each word gets a capital -->
<echo value="$name" /> <!-- ' ada LOVELACE ' because the text itself is unchanged -->
$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
文本中第一次出现某段文本之后的部分(使用 last 时为最后一次出现之后的部分),若未出现则返回整个文本:'a.tar.gz'->after('.', last) 的结果是 gz。
->after(text, last?) 返回第一次出现 text 之后的文本,使用 last 时返回最后一次出现之后的文本。当 text 不存在时,返回整个文本:
<let name="file" value="'archive.tar.gz'" />
<echo value="$file->after('.')" /> <!-- 'tar.gz' because it cuts at the first dot -->
<echo value="$file->after('.', last)" /> <!-- 'gz' because it cuts at the last dot -->
<echo value="'no-at-sign'->after('@')" /> <!-- 'no-at-sign' because there is no @ to cut at -->
$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
文本中第一次出现某段文本之前的部分(使用 last 时为最后一次出现之前的部分),若未出现则返回整个文本:'a.tar.gz'->before('.', last) 的结果是 a.tar。
->before(text, last?) 返回第一次出现 text 之前的文本,使用 last 时返回最后一次出现之前的文本。当 text 不存在时,返回整个文本:
<let name="email" value="'joan@example.com'" />
<echo value="$email->before('@')" /> <!-- 'joan' -->
<echo value="'archive.tar.gz'->before('.', last)" /> <!-- 'archive.tar' because it cuts at the last dot -->
$email = 'joan@example.com'
echo $email->before('@') // 'joan'
echo 'archive.tar.gz'->before('.', last) // 'archive.tar' because it cuts at the last dot
<let name="pantry">```text
tea
milk
flour
sugar
eggs
```</let>
<let name="shelves" value="$pantry->blocks()" />
<echo value="$shelves->count()" /> <!-- 2 because one blank line splits the text in two -->
<echo value="$shelves->map(.lines()->count())" /> <!-- [2, 3] because the first block has 2 lines and the second 3 -->
$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
<echo value="'élan and style'->capitalize()" /> <!-- 'Élan and style' because only the first letter changes -->
<echo value="'iPhone'->capitalize()" /> <!-- 'IPhone' -->
echo 'élan and style'->capitalize() // 'Élan and style' because only the first letter changes
echo 'iPhone'->capitalize() // 'IPhone'
<echo value="'tea'->chars()" /> <!-- ['t', 'e', 'a'] -->
<echo value="'👍🏽x'->chars()->count()" /> <!-- 2 because the thumb and its skin tone are one character -->
echo 'tea'->chars() // ['t', 'e', 'a']
echo '👍🏽x'->chars()->count() // 2 because the thumb and its skin tone are one character
<let name="garden">```text
#.#
..#
```</let>
<let name="plot" value="$garden->grid()" />
<echo value="$plot" /> <!-- [['#', '.', '#'], ['.', '.', '#']] -->
<echo value="$plot[0][1]" /> <!-- '.' because row 0 is #.# and column 1 is its middle -->
<echo value="$plot[1][2]" /> <!-- '#' because row 1 is ..# and column 2 is its last -->
$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
<let name="body" value="'{"id": 1}'" />
<let name="signature" value="$body->hmac('whsec_test')" />
<echo value="$signature->count()" /> <!-- 64 because SHA-256 in hex is 64 characters -->
<echo value="@secure_equals($signature, $body->hmac('whsec_test'))" /> <!-- true because the same key signs the same text the same way -->
$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
参数
接受
key必填
任意值
algorithm可选
以下单词之一:sha256|sha1|sha512
参数可以命名:->hmac(…, algorithm: …)。
示例
<echo>{ '{"id": 1}'->hmac('whsec_test') }</echo>
echo '{"id": 1}'->hmac('whsec_test')
#string->index_of(text, ignore_case?: bool, last?: bool)→ number
<echo value="'42'->is_numeric()" /> <!-- true -->
<echo value="' -3.5 '->is_numeric()" /> <!-- true because the spaces around it are fine -->
<echo value="'42 cats'->is_numeric()" /> <!-- false because cats isn't part of a number -->
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
<let name="note">```text
tea
milk
juice
```</let>
<echo value="$note->lines()" /> <!-- ['tea', 'milk', '', 'juice'] -->
<echo value="$note->lines()->count()" /> <!-- 4 because the blank line is a line too -->
$note = ```text
tea
milk
juice
```
echo $note->lines() // ['tea', 'milk', '', 'juice']
echo $note->lines()->count() // 4 because the blank line is a line too
<echo value="'4242424242424242'->mask('*', keep_end: 4)" /> <!-- '************4242' because only the last 4 show -->
<echo value="'secret'->mask()" /> <!-- '******' -->
echo '4242424242424242'->mask('*', keep_end: 4) // '************4242' because only the last 4 show
echo 'secret'->mask() // '******'
<echo value="'Tea'->matches(/^[a-z]+$/i)" /> <!-- true because i ignores case -->
<echo value="'Tea2'->matches(/^[a-z]+$/i)" /> <!-- false because 2 isn't a letter -->
echo 'Tea'->matches(/^[a-z]+$/i) // true because i ignores case
echo 'Tea2'->matches(/^[a-z]+$/i) // false because 2 isn't a letter
->normalize_unicode(form?) 使外观相同的文本比较结果相同。é 可以存储为一个字符,也可以存储为 e 加一个重音符;NFC(默认)将它们合并,NFD 则将它们拆开:
<echo value="'café'->normalize_unicode(NFD)->count()" /> <!-- 5 because NFD stores é as e and an accent -->
<echo value="'café'->normalize_unicode()->count()" /> <!-- 4 -->
echo 'café'->normalize_unicode(NFD)->count() // 5 because NFD stores é as e and an accent
echo 'café'->normalize_unicode()->count() // 4
<echo value="'move 3 boxes from -1 to 12'->numbers()" /> <!-- [3, -1, 12] because -1 has a space before its minus -->
<echo value="'pages 4-9'->numbers()" /> <!-- [4, 9] because the minus joins 4 and 9, so it is a dash -->
<echo value="'price 2.50, qty 4'->numbers()" /> <!-- [2.5, 4] -->
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]
<echo value="'7'->pad(3, '0', start)" /> <!-- '007' because two zeros go in front -->
<echo value="'tea'->pad(5, '.')" /> <!-- 'tea..' -->
<echo value="'teapot'->pad(3)" /> <!-- 'teapot' because it is already longer than 3 -->
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
将每个 old 替换为 new 后的文本。old 可以是正则表达式:->replace(/\s+/, ' '),new 可以用其分组,写作 $1 或 ${name}。使用 ignore_case 时,普通文本匹配时不区分大小写。
->replace(old, new, ignore_case?) 将每个 old 替换为 new。old 可以是正则表达式,此时 new 可以用 $1 或 ${name} 引用其分组所匹配的内容:
<echo value="'banana'->replace('a', 'o')" /> <!-- 'bonono' because every a changes -->
<echo value="'Tea'->replace('t', 'p', ignore_case)" /> <!-- 'pea' because T matches t -->
<echo value="'too many spaces'->replace(/\s+/, ' ')" /> <!-- 'too many spaces' -->
<echo value="'2026-12-01'->replace(/(\d+)-(\d+)-(\d+)/, '$3/$2/$1')" /> <!-- '01/12/2026' because the groups swap places -->
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
<echo value="'tea'->sha256()->count()" /> <!-- 64 -->
<echo value="'tea'->sha256() == 'tea'->sha256()" /> <!-- true because the same text gives the same hash -->
<echo value="'tea'->sha256() == 'Tea'->sha256()" /> <!-- false because one letter changed -->
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
<let name="orchard">```text
#.#.#
.#...
#.#.#
```</let>
<type name="Tree" extends="Mask">
<field name="layout" default="```text
#.#
.#.
```" />
<field name="symbols" default="{ '.': any }" />
</type>
<echo value="$orchard->shapes(Tree)->count()" /> <!-- 1 because the V of three trees fits only at the top left -->
<echo value="$orchard->shapes(Tree, rotated)->count()" /> <!-- 4 because turned upside down and on its sides it fits three more places -->
<echo value="$orchard->shapes(Tree)->first().points->map(.column)" /> <!-- [0, 2, 1] because the points come in reading order: the top two, then the middle -->
<type name="Instructions" description="A printer's instructions: one or more colours." extends="Mask">
<field name="layout" default="/[roygbv]+/" />
</type>
<echo value="'rgbrgbrgbr'->shapes(Instructions, repeated)->first().points->count()" /> <!-- 3 because rgb, repeated and cut off, prints the whole barcode -->
<echo value="'?rv'->shapes(Instructions, repeated, blank: '?')->first().points->count()" /> <!-- 2 because vr fits: the ? is a v -->
$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
/// A printer's instructions: one or more colours.
type Instructions extends Mask {
layout: /[roygbv]+/
}
echo 'rgbrgbrgbr'->shapes(Instructions, repeated)->first().points->count() // 3 because rgb, repeated and cut off, prints the whole barcode
echo '?rv'->shapes(Instructions, repeated, blank: '?')->first().points->count() // 2 because vr fits: the ? is a v
<let name="word" value="'teapot'" />
<echo value="$word->slice(0, 3)" /> <!-- 'tea' because it stops before position 3 -->
<echo value="$word->slice(3)" /> <!-- 'pot' -->
<echo value="$word->slice(-3)" /> <!-- 'pot' because -3 is three 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
<echo value="'tea,milk,juice'->split(',')" /> <!-- ['tea', 'milk', 'juice'] -->
<echo value="'tea milk'->split()" /> <!-- ['tea', 'milk'] -->
<echo value="'a, b,c'->split(/\s*,\s*/)" /> <!-- ['a', 'b', 'c'] because the spaces around each comma go too -->
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
<echo value="'v1.2'->strip(prefix: 'v')" /> <!-- '1.2' -->
<echo value="'notes.txt'->strip(suffix: '.txt')" /> <!-- 'notes' -->
<echo value="'[tea]'->strip(prefix: '[', suffix: ']')" /> <!-- 'tea' -->
<echo value="'tea'->strip(prefix: 'x')" /> <!-- 'tea' because there is no x to take off -->
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
<echo value="'tea'->to_base64()" /> <!-- 'dGVh' -->
<echo value="'tea?'->to_base64()" /> <!-- 'dGVhPw==' -->
<echo value="'tea?'->to_base64(url)" /> <!-- 'dGVhPw' because url leaves off the padding -->
<echo value="'tea'->to_base64()->decode()" /> <!-- 'tea' because the base64 value decodes back to the text -->
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
<let name="day" value="'2026-11-22'->to_date()" />
<echo value="$day.year" /> <!-- 2026 -->
<echo value="'22-11-2026'->to_date(dd-MM-yyyy).month" /> <!-- 11 because the pattern says the month is in the middle -->
$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
<echo value="'42'->to_int()" /> <!-- 42 -->
<echo value="'3.7'->to_int()" /> <!-- 3 because the .7 is dropped -->
<echo value="'abc'->to_int(-1)" /> <!-- -1 because abc isn't a number -->
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
<echo value="'3.7'->to_number()" /> <!-- 3.7 -->
<echo value="'x'->to_number(0)" /> <!-- 0 because x isn't a number, so the default is used -->
<echo value="'x'->to_number(null)" /> <!-- null -->
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
<echo value="'Hello, world!'->words()" /> <!-- ['Hello', 'world'] -->
<echo value=""Don't stop"->words()->count()" /> <!-- 2 because don't is one word -->
echo 'Hello, world!'->words() // ['Hello', 'world']
echo "Don't stop"->words()->count() // 2 because don't is one word
示例
<echo>{ 'Hello, world! A well-known phrase.'->words() }</echo>
echo 'Hello, world! A well-known phrase.'->words()
<let name="lines" value="'Ann went home early'->wrap(width: 9)->lines()" />
<echo value="$lines" /> <!-- ['Ann went', 'home', 'early'] because home early would be 10 characters -->
$lines = 'Ann went home early'->wrap(width: 9)->lines()
echo $lines // ['Ann went', 'home', 'early'] because home early would be 10 characters