Often when writing PHP I'll have it output some HTML like this -
echo "<a href="../" title="link title">".$link_text."</a>";
Obviously this won't parse as I need to escape the double quotes in the attributes of the <a>
element. Is there a regex that would quickly do this rather than me manually adding the backslashes?
One other thing - the regex shouldn't escape double quotes outside of the tag (e.g. where I've appended the $link_text
variable.
Any ideas?
Solutions I can come up with (not without escaping):
Single quotes
Use double quotes
Sprintf
Use HEREDOC
Use template engine like smarty
Exit PHP-mode:
BTW, be sure to use
htmlspecialchars()
on$link_text
variable, or you’ll have a XSS security hole.use single quotes or use heredoc. I'd prefer the last.
You should just use single-quotes instead:
I think you can use
try it.
Use (This syntax dont worry about quotes etc)
I'd strongly suggest using templating instead of trying to build strings.
In raw PHP: