Format / Safe String for the "title" attribute in the anchor

I have a function that builds an anchor tag. The function receives the URL, Title as parameters. The problem is that once the text includes quotation marks, and this leads to an anchor tag generated with syntax errors.

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

Otherwise, I can check the string and remove all the quotes, but I would like to know if there is a better way to do this, for example, there may be some other characters that could also cause my function to crash.

+6
source share
1 answer

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

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

+8
source

Source: https://habr.com/ru/post/886148/


All Articles