Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TWIG - Remove Everything After Certain Character

Tags:

twig

In Twig, I am wondering if it is possible to not render everything after a certain character. For example, if I had the string 'Ironman 3 : Marvel' and I want to strip everything after the semicolon so my string will become 'Ironman 3'. I am wondering if this is possible?

like image 576
Jon Avatar asked Jun 25 '13 18:06

Jon


People also ask

How do I remove all characters from a string after a specific character?

To remove everything after a specific character in a string:Use the String. split() method to split the string on the character.

How do I trim a string after a specific character in PHP?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.

How do you split a string in twig?

If the delimiter is an empty string, then value will be split by equal chunks. Length is set by the limit argument (one character by default). Internally, Twig uses the PHP explode or str_split (if delimiter is empty) functions for string splitting.

How do you remove the string after a hyphen?

Use the replaceAll() method to remove all hyphens from a string, e.g. const hyphensRemoved = str. replaceAll('-', ''); . The replaceAll method will remove all hyphens from the string by replacing them with empty strings.


1 Answers

You should be able to do this with some variation of {{ movietitle|split(':')[0] }}

like image 72
faffaffaff Avatar answered Sep 30 '22 19:09

faffaffaff