Try using the htmlentities() function in PHP as follows,<br />
echo htmlentities($str);<br />
it will automatically replace special characters by the corresponding HTML entities.<br />
* For more info - htmlentities()