9951 explained code solutions for 126 technologies


twigHow to trim a string in PHP Twig?


Trimming a string in PHP Twig is a simple task. You can use the trim filter to remove whitespace from the beginning and end of a string.

For example:

{{ '  Hello World  '|trim }}

Output example

Hello World

The trim filter takes two optional parameters: character_mask and side.

  • character_mask: A list of characters to be stripped.
  • side: Specifies which side of the string to trim. Possible values are left, right, or both (default).

Helpful links

Edit this code on GitHub