String filters

String filters are used to manipulate outputs and variables of the string type.

append

Appends characters to a string.

camelcase

Converts a string into CamelCase.

capitalize

Capitalizes the first word in a string.

downcase

Converts a string into lowercase.

escape

Escapes a string.

handle/handleize

Formats a string into a handle.

The string is converted to lowercase, and any spaces and special characters replaced by hyphens (-). If a title contains multiple consecutive spaces or special characters, then they're replaced by a single hyphen. Spaces and special characters are trimmed off of the beginning and end of the handle.

md5

Converts a string into an MD5 hash.

An example use case for this filter is showing the Gravatar image associated with the poster of a blog comment:

sha1

Converts a string into a SHA-1 hash.

sha256

Converts a string into a SHA-256 hash.

hmac_sha1

Converts a string into a SHA-1 hash using a hash message authentication code (HMAC). Pass the secret key for the message as a parameter to the filter.

hmac_sha256

Converts a string into a SHA-256 hash using a hash message authentication code (HMAC). Pass the secret key for the message as a parameter to the filter.

newline_to_br

Inserts a <br> linebreak HTML tag in front of each line break in a string.

pluralize

Outputs the singular or plural version of an English string based on the value of a number. The first parameter is the singular string and the second parameter is the plural string.

prepend

Prepends characters to a string.

remove

Removes all occurrences of a substring from a string.

remove_first

Removes only the first occurrence of a substring from a string.

replace

Replaces all occurrences of a string with a substring.

replace_first

Replaces the first occurrence of a string with a substring.

slice

The slice filter returns a substring, starting at the specified index. An optional second parameter can be passed to specify the length of the substring. If no second parameter is given, a substring of one character will be returned.

If the passed index is negative, it is counted from the end of the string.

split

The split filter takes on a substring as a parameter. The substring is used as a delimiter to divide a string into an array. You can output different parts of an array using array filters.

strip

Strips tabs, spaces, and newlines (all whitespace) from the left and right side of a string.

lstrip

Strips tabs, spaces, and newlines (all whitespace) from the left side of a string.

rstrip

Strips tabs, spaces, and newlines (all whitespace) from the right side of a string.

strip_html

Strips all HTML tags from a string.

strip_newlines

Removes any line breaks/newlines from a string.

truncate

Truncates a string down to the number of characters passed as the first parameter. An ellipsis (...) is appended to the truncated string and is included in the character count.

Custom ellipsis

truncate takes an optional second parameter 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 parameter counts against the number of characters specified by the first parameter. For example, if you want to truncate a string to exactly 10 characters, and use a 3-character ellipsis, use 13 for the first parameter of truncate, since the ellipsis counts as 3 characters.

No ellipsis

You can truncate to the exact number of characters specified by the first parameter and show no trailing characters by passing a blank string as the second parameter:

truncatewords

Truncates a string down to the number of words passed as the first parameter. An ellipsis (...) is appended to the truncated string.

Custom ellipsis

truncatewords takes an optional second parameter 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 parameter:

upcase

Converts a string into uppercase.

url_encode

Converts any URL-unsafe characters in a string into percent-encoded characters.

Note that url_encode will turn a space into a + sign instead of a percent-encoded character.

url_escape

Identifies all characters in a string that are not allowed in URLS, and replaces the characters with their escaped variants.

url_param_escape

Replaces all characters in a string that are not allowed in URLs with their escaped variants, including the ampersand (&).

Reversing strings

reverse cannot be used directly on a string, but you can split a string into an array, reverse the array, and rejoin it by chaining together other filters: