tif
tif

Reputation: 1149

Format/Safe string for "title" attribute in anchor

I have a function that builds an anchor tag. The function recieves the URL, Title as parameters. The problem is that sometime the text includes quotation marks and this results in a anchor tag generated with syntax errors.

Which is the best way to solve this problems? Is there any function that parses the text into a safe string, in this case, for the title attribute.

Otherwise I can check the string and strip all quotation marks, but I would like know if there is a better way to do this, e.g there might be some other characters that can crash my function as well.

Upvotes: 6

Views: 1292

Answers (1)

Joshua
Joshua

Reputation: 8212

Actually you want to use HttpUtility.HtmlAttributeEncode to encode your title attribute. The other encoders will do more work (and have different uses) whereas this one only escapes ", &, and < to generate a valid text for an attribute.

Example: This is a <"test"> & something else. becomes This is a &lt;&quot;Test&quot;> &amp; something else.

Upvotes: 8

Related Questions