title: PHP Simple Template Engine / Function
body: <p>I'm required to create a simple template engine; I can't use Twig or Smarty, etc. because the designer on the project needs to be able to just copy/paste her HTML into the template with no configuration, muss/fuss, whatever. It's gotta be <strong>really</strong> easy.</p>
  
  <p>So I created something that will allow her to do just that, by placing her content between <code>{{ CONTENT }}</code> <code>{{ !CONTENT }}</code> tags.</p>
  
  <p>My only problem is that I want to make sure that if she uses multiple spaces in the tags - or NO spaces - it won't break; i.e. <code>{{   CONTENT   }}</code> or <code>{{CONTENT}}</code></p>
  
  <p>What I have below accomplishes this, but I'm afraid it may be overkill. Anybody know a way to simplify this function?</p>
  
  <pre><code>function defineContent($tag, $string) {
  
      $offset = strlen($tag) + 6;
  
      // add a space to our tags if none exist
      $string = str_replace('{{'.$tag, '{{ '.$tag, $string);
      $string = str_replace($tag.'}}', $tag.' }}', $string);
  
      // strip consecutive spaces
      $string = preg_replace('/\s+/', ' ', $string);
  
      // now that extra spaces have been stripped, we're left with this
      // {{ CONTENT }} My content goes here {{ !CONTENT }}
  
      // remove the template tags
      $return = substr($string, strpos($string, '{{ '.$tag.' }}') + $offset);
      $return = substr($return, 0, strpos($return, '{{ !'.$tag.' }}'));
  
      return $return;
  }
  
  // here's the string
  $string  = '{{     CONTENT  }} My content   goes here  {{ !CONTENT   }}';
  
  // run it through the function
  $content = defineContent('CONTENT', $string);
  
  echo $content;
  
  // gives us this...
  My content goes here
  </code></pre>
  
  <h3>EDIT</h3>
  
  <p>Ended up creating a repo, for anyone interested.</p>
  
  <p><a href="https://github.com/timgavin/tinyTemplate" rel="nofollow noreferrer">https://github.com/timgavin/tinyTemplate</a></p>