title: str_replace failing with specific characters
body: <p>Running a simple str_replace that seems to have sound parts, but when I run the str_replace with these parts, the output is incorrect.</p>
  
  <pre><code>foreach($x-&gt;query("//img") as $node) 
  {   
      $absoluteImage = make_absolute($node-&gt;getAttribute("src"), $url);
      $iterateImage = str_replace($node-&gt;getAttribute("src"), $absoluteImage, $html);   
  }
  
  function make_absolute($url, $base) { 
  
      // Return base if no url
      if( ! $url) return $base;
  
      // Already absolute URL
      if(parse_url($url, PHP_URL_SCHEME) != '') return $url;
  
      // Only containing query or anchor
      if($url[0] == '#' || $url[0] == '?') return $base.$url;
  
      // Parse base URL and convert to local variables: $scheme, $host, $path
      extract(parse_url($base));
  
      // If no path, use /
      if( ! isset($path)) $path = '/';
  
      // Remove non-directory element from path
      $path = preg_replace('#/[^/]*$#', '', $path);
  
      // Destroy path if relative url points to root
      if($url[0] == '/') $path = '';
  
      // Dirty absolute URL
      $abs = "$host$path/$url";
  
      // Replace '//' or '/./' or '/foo/../' with '/'
      $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
      for($n = 1; $n &gt; 0; $abs = preg_replace($re, '/', $abs, -1, $n)) {}
  
      // Absolute URL is ready!
      return $scheme.'://'.$abs;
  }
  </code></pre>
  
  <p>The code is for a scraper that replaces the src portion of img tag from a relative to absolute URL. make_absolute is the function that does the work, and it works fine. When I echo out both $absoluteImage and $node->getAttribute("src") they both print the correct result. The absolute URL, and relative URL respectively. But when I run the str_replace function with these two parts, the result is incorrect. See the below data:</p>
  
  <p>The relative URL's:</p>
  
  <pre><code>&lt;img border="0" src="media/splash/a1.jpg" width="123" height="49"&gt;
  &lt;img border="0" src="media/splash/a2.jpg" width="136" height="49"&gt;
  &lt;img border="0" src="media/splash/pixel_clear.gif" width="187" height="1"&gt;&lt;br&gt;
  &lt;img border="0" src="media/splash/b1.jpg" width="123" height="66"&gt;
  &lt;img border="0" src="media/splash/b2.jpg" width="136" height="66"&gt;
  &lt;img border="0" src="media/splash/b3.jpg" width="187" height="66"&gt;
  &lt;img border="0" src="media/splash/c1.jpg" width="123" height="150"&gt;
  &lt;img border="0" src="media/splash/c2.jpg" width="136" height="150"&gt;
  &lt;img border="0" src="media/splash/c3.jpg" width="187" height="150"&gt;
  &lt;img border="0" src="media/splash/pixel_clear.gif" width="123" height="1"&gt;
  &lt;img border="0" src="media/splash/d1.jpg" width="136" height="127"&gt;
  &lt;img border="0" src="media/splash/d2.jpg" width="187" height="127"&gt;
  &lt;img border="0" src="media/splash/pixel_clear.gif" width="446" height="59"&gt;
  &lt;img border="0" src="media/splash/f1.jpg" width="446" height="147"&gt;&lt;br&gt;
  &lt;img border="0" src="media/splash/g1.jpg" width="446" height="142"&gt;&lt;/td&gt;
  </code></pre>
  
  <p>The resulting absolute URL's:</p>
  
  <pre><code>&lt;img border="0" src="http://www.bjnormand.com/media/splash/a1.jpg" width="123" height="49"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/a2.jpg" width="136" height="49"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/http://www.bjnormand.com/http://www.bjnormand.com/media/splash/pixel_clear.gif" width="187" height="1"&gt;&lt;br&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/b1.jpg" width="123" height="66"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/b2.jpg" width="136" height="66"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/b3.jpg" width="187" height="66"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/c1.jpg" width="123" height="150"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/c2.jpg" width="136" height="150"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/c3.jpg" width="187" height="150"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/http://www.bjnormand.com/http://www.bjnormand.com/media/splash/pixel_clear.gif" width="123" height="1"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/d1.jpg" width="136" height="127"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/d2.jpg" width="187" height="127"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/http://www.bjnormand.com/http://www.bjnormand.com/media/splash/pixel_clear.gif" width="446" height="59"&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/f1.jpg" width="446" height="147"&gt;&lt;br&gt;
  &lt;img border="0" src="http://www.bjnormand.com/media/splash/g1.jpg" width="446" height="142"&gt;
  </code></pre>
  
  <p>The only thing I can see in this is that the 3 lines that are failing have an '_' in the image name.</p>
  
  <p>Anyone have an idea of what's going on?</p>
  
  <p><strong>UPDATE:</strong>
  Tried this to keep tabs on each iteration, but no luck yet:</p>
  
  <pre><code>$count = 0;
  $imgArr = array();
  foreach($x-&gt;query("//img") as $node) 
  {
      if ($node-&gt;getAttribute("src")) {
          $imgArr[] = $node-&gt;getAttribute("src");
          $count++;
      }
  }
  for($i = 0; $i &lt; count($imgArr); $i++) {
      $html = str_replace($imgArr[$i], make_absolute($imgArr[$i], $url), $html);
  }
  
  echo $html;
  </code></pre>