String helpers
The text a page shows is often made in its PHP first: a title cut to fit a card, a column name turned into a label, a slug for a database column, a token for a mail. PHP's own substr and ucwords count bytes and know only ASCII; the padStr functions count characters, so an accented letter or an emoji is never cut in half. They are plain functions, loaded on every request like the rest of pad/lib/.
| function | what it answers |
|---|---|
padStrLimit ( $text, $limit = 100, $end = '...' )
|
at most $limit characters, $end added when something was cut
|
padStrWords ( $text, $words = 100, $end = '...' )
|
at most $words words
|
padStrExcerpt ( $text, $phrase, $radius = 100, $omission = '...' )
|
the phrase with $radius characters on either side, found case-insensitively
|
padStrSquish ( $text )
|
trimmed, every run of whitespace one space |
padStrCamel, padStrStudly, padStrSnake ( $text, $delimiter = '_' ), padStrKebab, padStrHeadline
|
userName, UserName, user_name, user-name, User Name - from a name in any of those cases |
padStrTitle ( $text )
|
every word with a capital |
padStrSlug ( $text, $separator = '-' )
|
a readable URL part - what the slug pipe gives
|
padStrAfter, padStrAfterLast, padStrBefore, padStrBeforeLast ( $text, $search )
|
the text after or before the first or last occurrence - what the pipes of those names give |
padStrBetween ( $text, $from, $to )
|
what stands between the first $from and the last $to
|
padStrIs ( $pattern, $value )
|
whether the value matches a pattern with * wildcards, or one of a list
|
padStrMask ( $text, $character, $index, $length = NULL )
|
a stretch of the text replaced by one character |
padStrPlural ( $word, $count = 2 ), padStrSingular ( $word )
|
the English plural or singular of a word |
padStrRandom ( $length = 16 )
|
random letters and digits |
padStrUuid ( $version = 4 )
|
a version 4 (random) or version 7 (time-ordered) UUID |
Shortening text
padStrLimit cuts at a number of characters and padStrWords at a number of words; both trim the space the cut leaves, add $end only when something was cut, and give a text that fits back as it is. padStrExcerpt is the snippet of a search result: the place the phrase stands, with the text around it. padStrSquish makes text pasted into a form field compare and store the same way, tabs, newlines and no-break spaces included.
|
PHP fragments/strings_1.php |
HTML fragments/strings_1.pad |
Result |
|---|---|---|
|
|
PAD templates drive... PAD templates drive the... ...ow: data, logic and outpu... [data, logic and output] |
Names in every case
The case functions read the words of a name whatever case it is in - an underscore, a dash, a space or a change of case separates them - and write them in the case asked for, so created_at, createdAt and CreatedAt all turn into each other. padStrHeadline makes a label of a column name; padStrTitle recases a sentence and leaves the rest of it as it is.
|
PHP fragments/strings_2.php |
HTML fragments/strings_2.pad |
Result | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
The Quick Brown Fox |
Slugs and cutting at a marker
padStrSlug is the slug pipe - the pipe calls it - so a slug stored when a post is saved is the slug a template makes for its link. The same holds for after, afterLast, before and beforeLast: a text without the search string comes back unchanged. padStrBetween takes what follows the first $from up to the last $to.
|
PHP fragments/strings_3.php |
HTML fragments/strings_3.pad |
Result |
|---|---|---|
|
|
creme-brulee-co-a-recipe - the pipe: creme-brulee-co-a-recipe shop | shop/orders | list.pad Orders |
Patterns and masks
padStrIs matches a value against a pattern in which * stands for any run of characters - a list of patterns matches when one of them does - without writing a regular expression. padStrMask hides part of a text: a negative index counts from the end, a negative length stops that many characters before it.
|
PHP fragments/strings_4.php |
HTML fragments/strings_4.pad |
Result |
|---|---|---|
|
|
admin/users: yes reports/may.pdf: yes reports/may.csv: no ***************1234 a********@example.com |
Plural and singular
padStrPlural gives the English plural of the last word of a text: the irregular words (person, child, mouse, knife ...), the uncountable ones (sheep, information, equipment ...) and the rules for -y, -s, -x, -ch and the rest. A count of one keeps the word, so "$n " . padStrPlural ( 'file', $n ) reads right for every number; an array or a Countable counts as its size. The case of the word is kept: Person, People; PERSON, PEOPLE. padStrSingular goes the other way.
|
PHP fragments/strings_5.php |
HTML fragments/strings_5.pad |
Result | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
0 items for 0 people 1 item for 1 person 2 items for 2 people
|
Random strings and UUIDs
padStrRandom draws letters and digits with random_int, PHP's secure generator - good for a token or a code to mail. padStrUuid makes a UUID as RFC 9562 writes it: version 4 is random; version 7 starts with the time, so ids made later sort later and a database index on them stays in order. The example shows their shape, as their value differs on every request.
|
PHP fragments/strings_6.php |
HTML fragments/strings_6.pad |
Result |
|---|---|---|
|
|
token: 32 letters and digits uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, versions 4 and 7 a later version 7 sorts after an earlier one: yes |
Wrong input
NULL and FALSE are read as an empty text and a number as its digits, so the functions take a database value as it comes. An array, a negative limit, an unknown UUID version or an empty mask character is a mistake of the author: it is reported as a PAD error naming the function, and the function answers an empty value.