Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape double quotes of HTML attributes output by PHP

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?

like image 873
Phil Avatar asked Nov 27 '22 12:11

Phil


1 Answers

You should just use single-quotes instead:

echo '<a href="../" title="link title">' . $link_text . '</a>';
like image 50
Greg Avatar answered Dec 06 '22 15:12

Greg