title: Generic PHP Match Template with String
body: <p>is there a function that will allow me to do the following:</p>
  
  <pre><code>$template = "{name}:{city}-{state}"
  $string = "Tom:Some CityPlace-CA"
  
  $out = function_I_am_looking_for($template,$string);
  </code></pre>
  
  <p>when $out returns</p>
  
  <pre><code>Array(
      [name] =&gt; Tom
      [city] =&gt; Some CityPlace
      [state] =&gt; CA
  )
  </code></pre>
  
  <p>Does such a function exist?</p>
  
  <p>-------- EDIT -----------</p>
  
  <p>So the people have spoken, and since I dont like seeing something like this die, I will conclude.  No built in function exists, however I did mock up one, and it does work.  Feel free to refine, please comment you edits.</p>
  
  <pre><code>function genaric_match($template,$string,$varStart="{{",$varEnd="}}"){
  
  
    $template = str_replace($varStart,"|~|`",$template);
    $template = str_replace($varEnd,"`|~|",$template);
  
    $t=explode("|~|",$template);
  
    $temp="";
    $i=0;
    foreach ($t as $n=&gt;$v){
      $i++;
      if (($i==count($t)||($i==(count($t)-1)&amp;&amp;$t[$n+1]==""))&amp;&amp;substr($v,0,1)=="`"&amp;&amp;substr($v,-1)=="`"){
        //Last Item
        $temp.="(?P&lt;".substr($v,1,-1)."&gt;.++)";
  
      }elseif(substr($v,0,1)=="`"&amp;&amp;substr($v,-1)=="`"){
        //Search Item
        $temp.="(?P&lt;".substr($v,1,-1)."&gt;[^".$t[$n+1]."]++)";
  
      }else{
        $temp.=$v;
      }
  
    }
    $temp="~^".$temp."$~";
  
    preg_match($temp, $string, $matches);
  
    return $matches;
  
  }
  </code></pre>
  
  <p>This example</p>
  
  <pre><code>print_r(genaric_match("{{name}}:{{city}}-{{state}}","Tom:Some CityPlace-CA"));
  </code></pre>
  
  <p>Returns</p>
  
  <pre><code>Array
  (
      [0] =&gt; Tom:Some CityPlace-CA
      [name] =&gt; Tom
      [1] =&gt; Tom
      [city] =&gt; Some CityPlace
      [2] =&gt; Some CityPlace
      [state] =&gt; CA
      [3] =&gt; CA
  )
  </code></pre>