title: Character replacement encoding php
body: <p>I have a string that I want to replace all 'a' characters to the greek 'α' character. I don't want to convert the html elements inside the string ie <code>&lt;a href="http://a-url-with-a-characters"&gt;text&lt;/a&gt;</code>.</p>
  
  <p>The function:</p>
  
  <pre><code>function grstrletter($string){
  
      $skip = false;
      $str_length = strlen($string);
  
      for ($i=0; $i &lt; $str_length; $i++){
  
          if($string[$i] == '&lt;'){
              $skip = true;
          }
  
          if($string[$i] == '&gt;'){
              $skip = false;
          }
  
          if ($string[$i]=='a' &amp;&amp; !$skip){
              $string[$i] = 'α';
          }
      }
  
      return $string;
  
  }
  </code></pre>
  
  <p>Another function I have made works perfectly but it doesn't take in account the hmtl elements.</p>
  
  <pre><code>function grstrletter_no_html($string){
  
   return strtr($string, array('a' =&gt; 'α'));
  
  }
  </code></pre>
  
  <p>I also tried a lot of encoding functions that php offers with no luck.</p>
  
  <p>When I echo the greek letter the browser output it without a problem. When I return the string the browser outputs the classic strange question mark inside a triangle whenever the replace was occured.</p>
  
  <p>My header has <code>&lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"&gt;</code> and I also tried it with php <code>header('Content-Type: text/html; charset=utf-8');</code> but again with no luck.</p>
  
  <p>The string comes from a database in UTF-8 and the site is in wordpress so I just use the wordpress functions to get the content I want. I don't think is a db problem because when I use my function <code>grstrletter_no_html()</code> everything works fine.</p>
  
  <p>The problem seems to happen when I iterate the string character by character.</p>
  
  <p>The file is saved as UTF-8 without BOM (notepad++). I tried also to change the encoding of the file with no luck again.</p>
  
  <p>I also tried to replace the greek letter with the corresponding html entity <code>&amp;#945;</code>     and <code>&amp;alpha;</code> but again same results.</p>
  
  <p>I haven't tried yet any regex.</p>
  
  <p>I would appreciate any help and thanks in advance.</p>
  
  <p>Tried: <a href="https://stackoverflow.com/questions/15398251/greek-characters-encoding-works-in-html-but-not-in-php?rq=1">Greek characters encoding works in HTML but not in PHP</a></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The solution based on <strong>deceze</strong> brilliant answer:</p>
  
  <pre><code>function grstrletter($string){
  
      $skip = false;
      $str_length = strlen($string);
  
      for ($i=0; $i &lt; $str_length; $i++){
  
          if($string[$i] == '&lt;'){
              $skip = true;
          }
  
          if($string[$i] == '&gt;'){
              $skip = false;
          }
  
          if ($string[$i]=='a' &amp;&amp; !$skip){
              $part1 = substr($string, 0, $i);
              $part1 = $part1 . 'α';
              $string = $part1 . substr($string, $i+1);
          }
      }
  
      return $string;
  
  }
  </code></pre>