title: Getting value in loop and replace it in string once in PHP
body: <p>I'm trying to find &amp; replace my values from inside string after getting values from inside loop.
  When I replace my string from inside loop then(that method isn't suitable)as it replaces one by one until loop ends and I get a lot of strings with single replacements at each.
  I'm trying to replace the whole string with loop values outside only once.
  Here's my code.</p>
  <pre><code>$str = &quot;wi are checking it&quot;;   
  $langs = array ('w', 'c', 'i');
  foreach ($langs as $lang) {
      $search = $lang;  
      $url[] = &quot;&lt;span style='color:red;'&gt;&quot;.$search.&quot;&lt;/span&gt;&quot;;
      $qw[] = $search;
  }
  $op = implode(&quot;&quot;, $url);
  $er = implode(&quot;&quot;, $qw);
  echo $op.&quot;&lt;br&gt;&quot;;
  echo $er.&quot;&lt;br&gt;&quot;;
  $new = str_replace($er, $op, $str);
  echo $new;
  </code></pre>
  <p>It's output:</p>
  <p><a href="https://i.stack.imgur.com/OnaJa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OnaJa.png" alt="Result" /></a></p>
  <p>Expected Output:</p>
  <p>[<img src="https://i.stack.imgur.com/MzK1m.png" alt="Expected Result" /></p>