cavill
cavill

Reputation: 610

Is it possible to extract the second word in a string in liquid?

I have a site (dsgnjbs.com), that displays a list of tweets. It uses the Liquid templating language. Changes in the Twitter API mean I can no longer auto-tweet with @mentions, which is causing some formatting errors. So:

I need to extract the 2nd word of each tweet and set it as a variable. I know liquid has various ways it can process text, such as

{{ tweet.text | twitter_autolink | remove_first: 'On' | remove_first: '@' | remove: ':' | remove: '#design' | remove: '#job' }}

But is it possible to specifically target the second word of a string?

Upvotes: 1

Views: 2301

Answers (1)

Donn Edwards
Donn Edwards

Reputation: 164

Use the "split" function.

{% assign words = tweet.text | split: ' ' %}
{{ words[1] }}

This gives you the second word, assuming that you use a space as a word separator. See http://wiki.shopify.com/Split

Upvotes: 4

Related Questions