<p>It sounds like you'd be better off using <a href="https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n#message-formatting" rel="nofollow noreferrer">Yii's Message Translations</a> for this, if that's possible in your situation. They are what powers Craft's <code>|t</code> / <code>|translate</code> filter:</p>

<pre><code>{{ "String with {something} and {somethingElse}"|t('site', entry) }}
</code></pre>

<p>With this, <code>{something}</code> and <code>{somethingElse}</code> would be replaced with the string values of <code>entry.something</code> and <code>entry.somethingElse</code>.</p>

<p>You can also pass an array for more complex substitutions:</p>

<pre><code>{{ "String with {something} and {somethingElse}"|t('site', {
    something: entry.something,
    somethingElse: entry.somethingElse,
}) }}
</code></pre>

CC BY-SA 4.0
2019-02-04T16:10:32.817Z
2019-02-04T16:21:19.827Z
2019-02-04T16:21:19.827Z
Mike Pepper
<p>Could you help me with something?</p>

<p>In a frontend template, I'm trying to go through the string, find all substrings between double curly brackets <code>{{ something }}</code> using regular expressions and replace them with field values that have the substring in the name. I want it to work dynamically.</p>

<p>i.e. replace string <code>{{ something }}</code> with value of field <code>{{ entry.something }}</code>, replace string <code>{{ something-else }}</code> with value of field <code>{{ entry.something-else }}</code> ...etc.</p>

<p>I tried the replace filter, and I'm able to find the strings between curly brackets, but I don't know how to replace them with actual fields values.</p>

<p>Thanks</p>

<replace>
Replace substring in string with variable named as substring?
# Replace substring in string with variable named as substring?

(Asked by: username_0 on 2019-02-04)

Could you help me with something?

In a frontend template, I'm trying to go through the string, find all substrings between double curly brackets `{{ something }}` using regular expressions and replace them with field values that have the substring in the name. I want it to work dynamically.

i.e. replace string `{{ something }}` with value of field `{{ entry.something }}`, replace string `{{ something-else }}` with value of field `{{ entry.something-else }}` ...etc.

I tried the replace filter, and I'm able to find the strings between curly brackets, but I don't know how to replace them with actual fields values.

Thanks

**Tags:** replace

---

## 1 answers

### Answer by username_1 on 2019-02-04. Score: 4

It sounds like you'd be better off using Yii's Message Translations for this, if that's possible in your situation. They are what powers Craft's |t / |translate filter:
{{ "String with {something} and {somethingElse}"|t('site', entry) }}

With this, {something} and {somethingElse} would be replaced with the string values of entry.something and entry.somethingElse.
You can also pass an array for more complex substitutions:
{{ "String with {something} and {somethingElse}"|t('site', {
    something: entry.something,
    somethingElse: entry.somethingElse,
}) }}


---

