I have what I think is a rather simple statement here

{{ (content.field_url.0)|replace("http://","") }}

but then I get an error

Warning: strtr() expects parameter 1 to be string, array given in twig_replace_filter() (line 482 of vendor\twig\twig\lib\Twig\Extension\Core.php). twig_replace_filter(Array, 'http://', '') (Line: 57)

Changing it up a little

{{ (content.field_url.0)|replace('http://',"") }}

and then I get this error

Warning: Invalid argument supplied for foreach() in Drupal\Core\Asset\LibraryDiscoveryParser->buildByExtension() (line 148 of core\lib\Drupal\Core\Asset\LibraryDiscoveryParser.php).

One more change up

{{ (content.field_url.0)|replace({'http://':""}) }}

and this returns nothing, nada, no value at all.

  • You have to dig a little bit deeper: {{ content.field_url[0]['#url'] }}. You might also need to distinguish between internal and external URL. – leymannx Jun 15 '18 at 9:36
  • Since you want to remove characters at the beginnig of your string, you might want to give a try to the "trim" filter : {{ content.field_url.0|trim('http://', 'left') }} – romain ni Jun 15 '18 at 10:14
  • 1
    Always easier to preprocess than to operate on render arrays. – Kevin Jun 15 '18 at 13:21

Browse other questions tagged or ask your own question.