ModS
ModS

Reputation: 836

Escaping special characters from html

I am retrieving a string from a webpage via a web browser, however the text on the page has many quotation marks ' and ". I am trying to pull the webpage into a variable, then use the variable as the body of an email. Do I need to swap out every double quote for single quotes OR double up the quotes, or since it is from a web page -> into a string, will it be OK?

Upvotes: 0

Views: 256

Answers (2)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201538

There is no need for any special handling of quotation marks. If you have a fragment (or all) of an HTML document as a string, you generally must not escape the Ascii quotation marks " and ', since they may have special meaning as attribute delimiters, as e.g. in <div class="foo"> and escaping them would change the meaning. Within element content, in textual HTML format, as opposite to the DOM, they could be escaped as &quot; and &apos;, but there is no need for that.

Upvotes: 1

mikek3332002
mikek3332002

Reputation: 3562

  • You don't need to escape any "quote" characters because you're not editing source code. It's all in memory.

  • If you want rendered html, do nothing, eg If you want <b>Bold</b> to produce Bold.

  • If you want the markup to display in the email, eg <p>Text</p>.

    • Replace < with &lt;
    • Replace > with &gt;
    • Replace & with &amp;

Upvotes: 1

Related Questions