You can write try custom Twig_Extention
In yourmodule
create service
yourmodule.service.yml
services:
custom_replace:
class: Drupal\yourmodule\TwigExtension\Custom_Replace
arguments: ['@renderer']
tags:
- { name: twig.extension }
src/TwigExtension/Custom_Replace.php
<?php
namespace Drupal\user_temp\TwigExtension;Drupal\yourmodule\TwigExtension;
/**
* Class Custom_Replace.
*/
class Custom_Replace extends \Twig_Extension {
/**
* {@inheritdoc}
*/
public function getName() {
return 'custom_replace';
}
public function getFunctions() {
return [
new \Twig_SimpleFunction(
'custom_replace',
[$this, 'custom_replace'],
['is_safe' => ['html']]
),
];
}
/**
* {@inheritdoc}
* @param $search can a array [`word1`,`work2`] or string `word`
*/
public function custom_replace($string, $search, $replace = '') {
return str_ireplace($search, $replace, $string);
}
}
In template yourpage.html.twig
{{ custom_replace('word','',string) }}