title: How to add HTML tags around a variable if it contains a value from an array, and leave it as it is if it doesn't in Smarty?
body: <p>In Smarty, I have this variable:</p>
  <p><code>{assign var=newNAME value='SNMS Stros Mos KOP2 APC'}</code></p>
  <p>I have the following array:</p>
  <p><code>{$spec_array = ['Stros Mos', 'Monk Dup', 'Lip Mok']}</code></p>
  <p>I need to check if 'Stros Mos' in <code>$newNAME</code> is in the array. In my searches for &quot;how to check if a string contains a value from an array&quot;, I found that you could loop through the array. In this case, the condition is: &quot;if the value is contained within the array add <code>&lt;i&gt;&lt;/i&gt;</code> around it, if not leave it as it is&quot;. This is what I came up with:</p>
  <pre><code>{foreach from=$spec_array item=spec}
          {if $newNAME|strpos:$spec}
          &lt;span class=&quot;thumbName&quot;&gt;{$newNAME|replace:$spec:&quot;&lt;i&gt;$spec&lt;/i&gt;&quot; nofilter}&lt;/span&gt;
          {else}
          &lt;span class=&quot;thumbName&quot;&gt;{$newNAME}&lt;/span&gt;
          {/if}
          {/foreach}
  </code></pre>
  <p>The problem is that it outputs 3 <code>&lt;span&gt;</code>, as such:</p>
  <pre><code>SNMS &lt;i&gt;Stros Mos&lt;/i&gt; KOP2 APC
  SNMS Stros Mos KOP2 APC
  SNMS Stros Mos KOP2 APC
  </code></pre>
  <p>I understand why: it's because the items from the array are being looped, and if the condition is met - add italics around the value, if not just print it as it is. Because there are 3 values in the array I get 3 outputs. I only need the first output. <code>$newNAME</code> could be anything, and sometimes it will not contain any of the values from <code>$spec_array</code>. I feel like the foreach loop here doesn't work. I understand that this is a logical problem more than a coding problem, but I am really new at PHP and Smarty and I am not a great thinker. Any help appreciated.</p>