title: Replace multiple strings without overlapping
body: <p>I have PHP application in which I have to replace a whole bunch of strings with their respective links. Basically I get a list of possible replacements that looks something like that:</p>
  
  <pre><code>"Table 1" =&gt; "&lt;a href='SOME_LINK'&gt;Table 1&lt;/a&gt;"
  "Table 2" =&gt; "&lt;a href='SOME_LINK'&gt;Table 2&lt;/a&gt;"
  ...
  "Table 10" =&gt; "&lt;a href='SOME_LINK'&gt;Table 10&lt;/a&gt;"
  "Table 11" =&gt; "&lt;a href='SOME_LINK'&gt;Table 11&lt;/a&gt;"
  </code></pre>
  
  <p>I go through each pair and I call <code>str_replace</code> which each couple of values. The problem is that even on the first pass I catch the <code>Table 10</code> strings as well as the <code>Table 1</code> strings and I replace both which is wrong. So I end up with result that has something like <code>&lt;a href='SOME_LINK'&gt;Table 1&lt;/a&gt;0</code>. <code>Table 10</code> should not be replaced at all (unless later there's another pair that actually takes care of <code>Table 10</code>).</p>
  
  <p>Any ideas for a work around? I though of adding a blank space after the search part of the string however it's possible to have something in the text like <code>Table 1,</code> which would then not match at all. I also considered using regular expressions to replace but not sure if there's a syntax there that would fix the issue above.</p>
  
  <p>EDIT:
  To improve the reading comprehension, here's expected input and output for this problem.
  CONDITION:</p>
  
  <pre><code>"Table 1" =&gt; "&lt;a href='SOME_LINK'&gt;Table 1&lt;/a&gt;"
  "Table 10" =&gt; "&lt;a href='SOME_LINK'&gt;Table 10&lt;/a&gt;"
  </code></pre>
  
  <p>INPUT:</p>
  
  <pre><code>We have some text here.
  It has words like Table 1, Table 2 and also Table 10.
  Those need to be replaced.
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <pre><code>We have some text here.
  It has words like &lt;a href='SOME_LINK'&gt;Table 1&lt;/a&gt;, Table 2 and also &lt;a href='SOME_LINK'&gt;Table 10&lt;/a&gt;.
  Those need to be replaced.
  </code></pre>