You need to, um, I forget what it's called (edit: the term I was looking for was 'escape'), but you put a backslash before any quotes. So:
HTML Code:
< a href=\"url/blah.php\">This is a link</a>
However, I have never had to do that. Let me compare your code to something I've done. (just a sec)
edit: Hmmm, I haven't done anything quite like you (using replace). But, try this (single quotes) and see if you don't have to escape your template quotes that way:
PHP Code:
$replace = '$templateblah';