title: Without Regex: String Between Quotes?
body: <p>I'm creating a word-replacement script. I've run into a roadblock with ignoring strings between quotes and haven't been able to find a decent solution here that didn't involve Regex.</p>
  
  <p>I have a working snippet that cycles through every character in the string and figures out whether the most recent quotation was an opening or closing quote (Whether single or double) and ignores escaped quotes. The problem is that in order for it to provide a 100% accurate experience, it has to run every time the string changes (Because of how it works, it could change well over 60K times across a single function), and due to string length potential, the code takes too long even on a fairly short script.</p>
  
  <p>Is there a fast way to figure out whether a string is between open and close quotes (Single and double)? Ignoring escaped " and '. Or, do you have suggestions on how to optimize the snippet to make it run significantly faster? Removing this function, the process runs at almost the preferred speed (Instant).</p>
  
  <p>As an exercise, consider copying and pasting the snippet into the script with a variable containing text. For example $thisIsAQuote = "This is a quote."; And, from that point, everything should replace correctly, except $thisIsAQuote should retain its exact text.</p>
  
  <p>But here's the issue: Other solutions I've found will treat everything between "This is a quote." and ... $this->formatted[$i - 1] != " ... as if it's still between quotes. Because as far as those solutions are concerned, the last quote in "This is a quote." and the first quote in the if-check are open and close quotes. Another obvious issue is that some strings contain words with apostrophes. Apostrophes shouldn't be treated as single-quotes, but in all solutions I've found, they are.</p>
  
  <p>In other words, they're "unaware" solutions.</p>
  
  <pre><code>    $quoteClosed = true;
      $singleQuoteClosed = true;
  
      $codeLength = mb_strlen($this-&gt;formatted);
      if ($codeLength == false)
          return;
  
      for ($i = 0; $i &lt; $codeLength; $i++)
      {
          if ((!$quoteClosed || !$singleQuoteClosed) &amp;&amp; ($this-&gt;formatted[$i] == '"' || $this-&gt;formatted[$i] == "'"))
          {
              if (!$quoteClosed &amp;&amp; $this-&gt;formatted[$i - 1] != "\\")
                  $quoteClosed = true;
              else if (!$singleQuoteClosed &amp;&amp; $this-&gt;formatted[$i - 1] != "\\")
                  $singleQuoteClosed = true;
          }
          else if ($this-&gt;formatted[$i] == '"' &amp;&amp; ($i &lt;= 0 || $this-&gt;formatted[$i - 1] != "\\"))
          {
              if ($quoteClosed &amp;&amp; $singleQuoteClosed)
                  $quoteClosed = false;
          }
          else if ($this-&gt;formatted[$i] == "'" &amp;&amp; ($i &lt;= 0 || $this-&gt;formatted[$i - 1] != "\\"))
          {
              if ($singleQuoteClosed &amp;&amp; $quoteClosed)
                  $singleQuoteClosed = false;
          }
  
          if ($quoteClosed &amp;&amp; $singleQuoteClosed)
              $this-&gt;quoted[$i] = 0;
          else
              $this-&gt;quoted[$i] = 1;
      }
  </code></pre>
  
  <p>If there isn't a way to make the above more efficient, is there a non-Regex way to quickly replace all substrings in an array with substrings in a second array without missing any across an entire string?</p>
  
  <p>substr_replace and str_replace only seem to replace "some" pieces of the overall string, which is why the number of iterations are in place. It cycles through a while loop until either strpos deems a string nonexistent (Which it never seems to do ... I may be using it wrong), or it cycles through 10K times, whichever occurs first.</p>
  
  <p>Running the above snippet -once- per round would solve the speed issue, but that leaves the "full-replacement" issue and, of course, staying aware that it should avoid replacing anything within quotes.</p>
  
  <pre><code>    for ($a = 0; $a &lt; count($this-&gt;keys); $a++)
      {
          $escape = 0;
          if ($a &gt; count($this-&gt;keys) - 5)
              $this-&gt;formatted = $this-&gt;decodeHTML($this-&gt;formatted);
  
          while (strpos($this-&gt;formatted, $this-&gt;keys[$a]) !== false)
          {
              $valid = strpos($this-&gt;formatted, $this-&gt;keys[$a]);
              if ($valid === false || $this-&gt;quoted[$valid] === 1)
                  break;
  
              $this-&gt;formatted = substr_replace($this-&gt;formatted, $this-&gt;answers[$a], $valid, mb_strlen($this-&gt;keys[$a]));
              $this-&gt;initializeQuoted();
              $escape++;
  
              if ($escape &gt;= 10000)
                  break;
          }
  
          if ($a &gt; count($this-&gt;keys) - 5)
              $this-&gt;formatted = html_entity_decode($this-&gt;formatted);
      }
      $this-&gt;quoted = array();
      $this-&gt;initializeQuoted();
      return $this-&gt;formatted;
  </code></pre>
  
  <p>'keys' and 'answers' are arrays containing words of various lengths. 'formatted' is the new string with the changed information. 'initializeQuoted' is the above snippet. I use htmlentities and html_entity_decode to help get rid of whitespaces with key/answer replacements.</p>
  
  <p>Ignore the magic numbers (5s and 10K).</p>