diff --git a/README.md b/README.md index a2042d5..83c6e4b 100644 --- a/README.md +++ b/README.md @@ -275,6 +275,8 @@ replace | `(replace "hello world" "hello" "bye")` | `"bye world"` | Replace subs split | `(split "-" "a-b-c")` | `("a" "b" "c")` | Split string using [explode](https://www.php.net/manual/en/function.explode.php). join | `(join "-" "a" "b" "c")` | `"a-b-c"` | Join string together using [implode](https://www.php.net/manual/en/function.implode.php). format | `(format "%d %.2f" 56 4.5)` | `"56 4.50"` | Format string using [sprintf](https://www.php.net/manual/en/function.sprintf.php). +prefix? | `(prefix? "hello world" "hello")` | `true` | Return true if the first argument starts with the second argument. +suffix? | `(suffix? "hello world" "world")` | `true` | Return true if the first argument ends with the second argument. Note that support for multibyte characters in strings is limited because the provided functions do not use the [mbstring](https://www.php.net/manual/en/book.mbstring.php) extension. diff --git a/src/Lib/Strings.php b/src/Lib/Strings.php index 4b1679c..ff46572 100644 --- a/src/Lib/Strings.php +++ b/src/Lib/Strings.php @@ -48,5 +48,13 @@ class Strings implements ILib $env->set('format', new CoreFunc('format', 'Format the remaining arguments as string specified by the first argument.', 1, -1, fn (string $a, ...$b) => sprintf($a, ...$b) )); + + $env->set('prefix?', new CoreFunc('prefix?', 'Return true if the first argument starts with the second argument.', 2, 2, + fn (string $str, string $start) => substr($str, 0, strlen($start)) === $start + )); + + $env->set('suffix?', new CoreFunc('suffix?', 'Return true if the first argument ends with the second argument.', 2, 2, + fn (string $str, string $end) => substr($str, strlen($str) - strlen($end)) === $end + )); } }