title: Parsing plain text in such a way that will recognise a custom if statement
body: <p>I have the following string:</p>
  
  <pre><code>$string = "The man has {NUM_DOGS} dogs."
  </code></pre>
  
  <p>I'm parsing this by running it through the following function:</p>
  
  <pre><code>function parse_text($string)
  {
      global $num_dogs;
  
      $string = str_replace('{NUM_DOGS}', $num_dogs, $string);
  
      return $string;
  }
  
  parse_text($string);
  </code></pre>
  
  <p>Where <code>$num_dogs</code> is a preset variable. Depending on <code>$num_dogs</code>, this could return any of the following strings:</p>
  
  <ul>
  <li>The man has 1 dogs.</li>
  <li>The man has 2 dogs.</li>
  <li>The man has 500 dogs.</li>
  </ul>
  
  <p>The problem is that in the case that "the man has 1 dogs", <em>dog</em> is pluralised, which is undesired. I know that this could be solved simply by not using the <code>parse_text</code> function and instead doing something like:</p>
  
  <pre><code>if($num_dogs = 1){
      $string = "The man has 1 dog.";
  }else{
      $string = "The man has $num_dogs dogs.";
  }
  </code></pre>
  
  <p>But in my application I'm parsing more than just <code>{NUM_DOGS}</code> and it'd take a lot of lines to write all the conditions.</p>
  
  <p>I need a shorthand way which I can write into the initial <code>$string</code> which I can run through a parser, which ideally wouldn't limit me to just two true/false possibilities.</p>
  
  <p>For example, let </p>
  
  <pre><code>$string = 'The man has {NUM_DOGS} [{NUM_DOGS}|0=&gt;"dogs",1=&gt;"dog called fred",2=&gt;"dogs called fred and harry",3=&gt;"dogs called fred, harry and buster"].';
  </code></pre>
  
  <p>Is it clear what's happened at the end? I've attempted to initiate the creation of an array using the part inside the square brackets that's after the vertical bar, then compare the key of the new array with the parsed value of {NUM_DOGS} (which by now will be the $num_dogs variable at the left of the vertical bar), and return the value of the array entry with that key.</p>
  
  <p>If that's not totally confusing, is it possible using the preg_* functions?</p>