Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:

twig - PHP 7.4 trimming whitespace between string variables

Hey everyone,

I hope you're doing well. I've been working on my PHP 7.4 project and recently came across a task that involves trimming whitespace between string variables in Twig. I have a good understanding of Twig, but I'm not sure how to accomplish this specific task.

To provide some context, I have a Twig template where I'm generating dynamic strings by concatenating multiple variables. However, there are cases where there might be whitespace between these variables, and I want to remove it.

For example, let's say I have two variables: `{{ var1 }}` and `{{ var2 }}`. The output currently looks like "Hello World" (with a space between the words). But I want it to appear as "HelloWorld" (without any whitespace).

I have been searching for a solution, but so far, I haven't found any built-in Twig filter or function that specifically deals with trimming whitespace between string variables.

If any of you have encountered a similar situation or know how to achieve this, I would greatly appreciate your guidance. Is there a Twig filter or a method that can help me achieve this directly? Or do I have to resort to other PHP methods within my Twig template or manipulate the variables before passing them to Twig?

It would be great if you could provide a code example or point me in the right direction. I'm eager to learn and implement the best solution for this issue.

Thank you so much in advance for your help!

All Replies


Hi there,

I completely understand the issue you're facing with trimming whitespace between string variables in Twig. It can be a bit tricky to find a straightforward solution, but I'd be happy to share my personal experience and how I managed to deal with this.

In a recent project, I had a similar requirement to concatenate string variables in Twig while ensuring there was no whitespace in between them. After exploring various options, I found a neat approach by using the `replace` filter in Twig.

First, I assigned each variable to a separate Twig variable, let's say `{{ var1 }}` and `{{ var2 }}`. Then, I used the `replace` filter to substitute any whitespace character with an empty space.

Here's an example snippet to illustrate this:

{% set var1 = "Hello " %}
{% set var2 = " World" %}

{% set concatenated = var1 ~ var2 %}
{% set trimmed = concatenated|replace({'\\s+': ''}) %}

{{ trimmed }}

By utilizing the `replace` filter and a regular expression pattern, I removed all whitespace characters between the variables. In this case, the output would be "HelloWorld" as desired.

I found this method quite effective because it provides more flexibility. You can easily modify the regular expression pattern in the `replace` filter to cater to different whitespace scenarios if needed.

I hope this approach proves useful for your particular situation. Let me know if you have any further queries or require additional assistance. Happy coding!


Hey there,

I understand your frustration with trying to trim whitespace between string variables in Twig. I've encountered a similar situation before, and I managed to solve it through a combination of PHP methods and a custom Twig filter.

In my case, I had to concatenate multiple variables that had the potential for whitespace between them. What I did was use the `trim()` PHP function to remove any leading or trailing whitespace from each variable before passing them to Twig.

Here's an example code snippet to clarify:

$var1 = "Hello ";
$var2 = " World";

$var1 = trim($var1);
$var2 = trim($var2);

// Further code to pass these variables to Twig

By using `trim()`, I removed the whitespace around each variable. After that, I passed these modified variables to my Twig template for concatenation. In this way, I ensured that any whitespace between variables was also trimmed.

Additionally, you can create a custom Twig filter to trim whitespace in Twig directly. You can define the filter in your PHP code and then use it within your Twig templates.

Here's an example code snippet to define and use a custom Twig filter:

// Define the filter
$trimFilter = new Twig_Filter('trim', function ($value) {
return trim($value);

// Add the filter to your Twig environment

Then, within your Twig template, you can use the `trim` filter on your variables like this:

{{ var1|trim }}{{ var2|trim }}

This will ensure any whitespace between the variables is removed.

I hope this helps! Let me know if you have any further questions or need more clarification.

New to LearnPHP.org Community?

Join the community