 <p>Because you just want to generate a php file I would suggest using string/regex replace in your ide/editor</p> <p>going from</p> <pre><code>&lt;option value="a"&gt;A&lt;/option&gt; </code></pre> <p>to</p> <pre><code>&lt;option value="a" &lt;?= isset($array["a"]) ? "selected" : "" ?&gt;&gt;A&lt;/option&gt; </code></pre> <p>In vim (but other editors should support this too) you can do this with</p> <pre><code>:%s/value=\(".*"\)/value=\1 &lt;?= isset($array[\1]) ? "selected" : "" ?&gt;/g </code></pre> <p>where the part you search for is</p> <pre><code>value=\(".*"\) </code></pre> <p>and this is the part you replace</p> <pre><code>value=\1 &lt;?= isset($array[\1]) ? "selected" : "" ?&gt; </code></pre> <p>using </p> <pre><code>\([someregex]\) </code></pre> <p>you can 'store' some variable and with \1 you can output it in the replace part 1</p>