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?
To remove everything after a specific character in a string:Use the String. split() method to split the string on the character.
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.
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.
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.
You should be able to do this with some variation of {{ movietitle|split(':')[0] }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With