append
Concatenates two strings and returns the concatenated value.
append
can also be used with variables:
prepend
Adds the specified string to the beginning of another string.
prepend
can also be used with variables:
capitalize
Makes the first character of a string capitalized.
capitalize
only capitalizes the first character of a string, so later words are not affected:
upcase
Makes each character in a string uppercase. It has no effect on strings which are already all uppercase.
downcase
Makes each character in a string lowercase. It has no effect on strings which are already all lowercase.
strip
Removes all whitespace (tabs, spaces, and newlines) from both the left and right sides of a string. It does not affect spaces between words.
lstrip
Removes all whitespace (tabs, spaces, and newlines) from the left side of a string. It does not affect spaces between words.
rstrip
Removes all whitespace (tabs, spaces, and newlines) from the right side of a string. It does not affect spaces between words.
strip_newlines
Removes any newline characters (line breaks) from a string.
replace
Replaces every occurrence of the first argument in a string with the second argument.
replace_first
Replaces only the first occurrence of the first argument in a string with the second argument.
replace_last
Replaces only the last occurrence of the first argument in a string with the second argument.
remove
Removes every occurrence of the specified substring from a string.
remove_first
Removes only the first occurrence of the specified substring from a string.
remove_last
Removes only the last occurrence of the specified substring from a string.
truncate
Shortens a string down to the number of characters passed as an argument. If the specified number of characters is less than the length of the string, an ellipsis (…) is appended to the string and is included in the character count.
Basic Usage
Custom ellipsis
truncate
takes an optional second argument that specifies the sequence of characters to be appended to the truncated string. By default this is an ellipsis (…), but you can specify a different sequence.
The length of the second argument counts against the number of characters specified by the first argument. For example, if you want to truncate a string to exactly 10 characters, and use a 3-character ellipsis, use 13 for the first argument of truncate
, since the ellipsis counts as 3 characters.
No ellipsis
You can truncate to the exact number of characters specified by the first argument and avoid showing trailing characters by passing a blank string as the second argument:split
Divides a string into an array using the argument as a separator. split
is commonly used to convert comma-separated items from a string to an array.
truncatewords
Shortens a string down to the number of words passed as an argument. If the specified number of words is less than the number of words in the string, an ellipsis (…) is appended to the string.
Custom ellipsis
truncatewords
takes an optional second argument that specifies the sequence of characters to be appended to the truncated string. By default this is an ellipsis (…), but you can specify a different sequence.
No ellipsis
You can avoid showing trailing characters by passing a blank string as the second argument:normalize_whitespace
Replace any occurrence of whitespace with a single space.
number_of_words
Count the number of words in some text. This filter takes an optional argument to control the handling of Chinese-Japanese-Korean (CJK) characters in the input string:
- Passing
'cjk'
as the argument will count every CJK character detected as one word irrespective of being separated by whitespace. - Passing
'auto'
(auto-detect) works similar to'cjk'
but is more performant if the filter is used on a variable string that may or may not contain CJK chars.