Functions prefixed with str_ perform tasks that are useful when dealing with strings. The helper maps directly to the Str PHP class and its methods. For example:
{{ str_camel() }}The above is the PHP equivalent of the following:
<?= Str::camel() ?>Methods in camelCase should be converted to snake_case.
You may also apply the string functions as a Twig filter.
{{ ''|str_camel }}Limit the number of characters in a string.
{{ str_limit('The quick brown fox...', 100) }}To add a suffix when limit is applied, pass it as the third argument. Defaults to ....
{{ str_limit('The quick brown fox...', 100, '... Read more!') }}Limit the number of words in a string.
{{ str_words('The quick brown fox...', 100) }}To add a suffix when limit is applied, pass it as the third argument. Defaults to ....
{{ str_words('The quick brown fox...', 100, '... Read more!') }}Replace all occurrences of the search string with the replacement string.
// Outputs: Bob
{{ 'Alice'|str_replace('Alice', 'Bob') }}Convert a value to camelCase.
// Outputs: helloWorld
{{ str_camel('hello world') }}Convert a value to StudlyCase.
// Outputs: HelloWorld
{{ str_studly('hello world') }}Convert a value to snake_case.
// Outputs: hello_world
{{ str_snake('hello world') }}The second argument can supply a delimiter.
// Outputs: hello---world
{{ str_snake('hello world', '---') }}Gets the plural form of an English word.
// Outputs: chickens
{{ str_plural('chicken') }}Makes a string uppercase.
// Outputs: Hello I'm JACK
Hello I'm {{ 'Jack'|str_upper }}Makes a string lowercase.
// Outputs: Hello I'm jack
Hello I'm {{ 'JACK'|str_lower }}Makes a string's first character uppercase.
// Outputs: Hello I'm Jack
Hello I'm {{ 'jack'|str_ucfirst }}Makes a string's first character lowercase.
// Outputs: Hello I'm jack
Hello I'm {{ 'Jack'|str_lcfirst }}Repeats a string.
// Outputs: We are the best best best!
We are the {{ 'best '|str_repeat(3) }}!Pads a string to a certain length with another string from both sides.
// Outputs: ooxxxoo
{{ 'xxx'|str_pad_both(7, 'o') }}Pads a string to a certain length with another string from left side.
// Outputs: ooxxx
{{ 'xxx'|str_pad_left(5, 'o') }}Pads a string to a certain length with another string from right side.
// Outputs: xxxoo
{{ 'xxx'|str_pad_right(5, 'o') }}Reverses a string.
// Outputs: !dlrow olleH
{{ 'Hello world!'|str_reverse }}