Replace Comma with Space


I’m building a Filter and need the Categories to be exactly like this:
data-filter="category1 category2 category3"

The categories are stored in a Multiselect field and I try to get them like this:
<?php foreach ($project->categories()->split() as $category): ?><?= $category ?><?php endforeach ?>

which gives me this:
data-filter="category1, category2, category3"

Now I want to remove the commas but keep the spaces. I tried
but this removes the spaces too. I also tried to just add Space at the end of <?= $category ?> but this breaks the filter.

Remove the foreach loop, and do this instead:

echo implode( ' ', $project->categories()->split(',') );

split(',') creates an array from the comma separated list, implode() joins the array elements into a string using the given separator.

1 Like