Escape only single quotes (leave double quotes alone) with htmlspecialchars()

Luke Shaheen picture Luke Shaheen · May 14, 2012 · Viewed 11.6k times · Source

I know there are other ways of of escaping only single quotes (such as this answer), but it appears to me that there should be a way using htmlspecialchars().

According to the manual, it should be some combination of their constants, but based on their explanations, I don't see it.

Is it possible to escape only single quotes, leaving the double quotes alone, with htmlspecialchars()?

Answer

Norse picture Norse · May 14, 2012
str_replace("'", "\\'", $string);

There.

Or, use ENT_QUOTES

htmlspecialchars($string, ENT_QUOTES);