title: How to use preg_replace to translate Japanese to English from a remote website
body: <p>I'm trying to write a PHP script to translate Japanese cooking recipes from Japanese to English.  This is pet project and doesn't have to be perfect.  My tactic is..</p>
  <ol>
  <li>Fetch the website contents using file_get_contents</li>
  <li>Replacing some Japanese with English (mostly names of ingredients)</li>
  <li>Write the results to an HTML file</li>
  </ol>
  <p>I'm running this PHP script from the command line:</p>
  <pre><code>&lt;?php
  
      // Get contents of Japanese cooking website
      $url = 'http://recipe.igamono.jp/?eid=1077379';
      $context = stream_context_create(array('http' =&gt; array('header' =&gt; 'Accept-Charset: UTF-8, *;q=0')));
      $html = file_get_contents($url, false, $context);
  
      // Replace stuff
      $html = preg_replace('/right/s', 'foobar', $html); // works
      $html = preg_replace('/の/s', 'shazam!!', $html); // doesn't work
  
      // Write output to a file
      file_put_contents('output.html', $html);
  
  ?&gt;
  </code></pre>
  <p>I'm using Sublime Text to edit the file (translate.php), and I've made sure to save the file using: File / Save with Encoding / UTF-8</p>
  <p>When I run the script, everything works except for the replacement of の. No substitutions of の occur.</p>
  <p>However, this <em><strong>does</strong></em> work:</p>
  <pre><code>&lt;?php
      $html = &quot;one の two の&quot;;
      $html = preg_replace('/の/s', 'shazam!!', $html);
  
      file_put_contents('output.html', $html);
  ?&gt;
  </code></pre>
  <p>The output is:</p>
  <blockquote>
  <p>one shazam!! two shazam!!</p>
  </blockquote>
  <p>Any suggestions?  I know that this is a character encoding issue, but I just can't seem to get it working.</p>
  <p><strong>UPDATE:</strong></p>
  <p>Here's a modified version that tests the UTF-8 encoding on the $html variable:</p>
  <pre><code>&lt;?php
      // Get contents of Japanese cooking website
      $url = 'http://recipe.igamono.jp/?eid=1077379';
      $context = stream_context_create(array('http' =&gt; array('header' =&gt; 'Accept-Charset: UTF-8, *;q=0')));
      $html = file_get_contents($url, FILE_TEXT, $context);
  
      if(mb_detect_encoding($html, 'UTF-8')) print(&quot;Yep, UTF-8 it is.\n&quot;);
      if(! mb_detect_encoding($html, 'UTF-8', true)) print(&quot;Well, on second thought.. maybe not!\n&quot;);
  
  ?&gt;
  </code></pre>
  <p>The output is:</p>
  <blockquote>
  <p>Yep, UTF-8 it is.</p>
  <p>Well, on second thought.. maybe not!</p>
  </blockquote>
  <p><strong>MY SOLUTION</strong></p>
  <p>Here's one solution that I came up with:</p>
  <pre><code>&lt;?php
      // Get contents of Japanese cooking website
      $url = 'http://recipe.igamono.jp/?eid=1077379';
      $html = file_get_contents($url);
  
      // Convert HTML to UTF-8 from Japanese
      $html = mb_convert_encoding($html, &quot;UTF-8&quot;, &quot;EUC-JP&quot;);
  
      // Replace stuff
      $html = preg_replace('/right/s', 'foobar', $html);
      $html = preg_replace('/の/s', 'shazam!!', $html);
  
      // Convert HTML back to Japanese character encoding
      $html = mb_convert_encoding($html, &quot;EUC-JP&quot;, &quot;UTF-8&quot;);
  
      // Write HTML to a file
      file_put_contents('output.html', $html);
  ?&gt;
  </code></pre>