title: How to replace a term with str_replace(), only if it is not part of another word?
body: <p>I have a text file like the following:</p>
  
  <p>yyy.txt:</p>
  
  <pre><code>line1                     
  line2                      
  line3line4    
  line5line3    
  line3
  </code></pre>
  
  <p>Now I want to replace <code>line3</code> with <code>555</code> in the code below. But I only want to replace the line if <code>line3</code> is not part of another word, e.g. <code>XYline3</code> or <code>line3XY</code>.</p>
  
  <p>I tried to accomplish this with this code:</p>
  
  <pre><code>$filename="yyy.txt";
  $line="line3";
  
  file_put_contents($filename, str_replace($line , "555",  file_get_contents($filename)));
  </code></pre>
  
  <p>output:</p>
  
  <pre>line1
  
  line2
  
  <b>555</b>line4
  
  line5<b>555</b>
  
  <b>555</b>
  </pre>
  
  <p>As you can see here it also replaced <code>line3line4</code> and <code>line5line3</code> even though I don't want this.</p>
  
  <p>So how can I change my current code, that it only replaces the search term if it is not part of another word? I'm kinda stuck here and not sure if this is possible with <code>str_replace()</code> or if I have to do it in another way.</p>