htmlhtml-entitieshtmltidy

Proper Way to Escape the | Character Using HTML Entities


To escape the ampersand character in HTML, I use the & HTML entity, for example:

<a href="https://www.example.com/?q1=val1&amp;q2=val2">Link</a>

If I have the following code in my HTML, how would I escape the | character?

<a href="https://www.example.com/?q1=val1|q2=val2">Link</a>

HTML Tidy is complaining, claiming an illegal character was found in my HTML.

I tried using &brvbar; and several other HTML entities, but Tidy says "malformed URI reference."


Solution

  • You wouldn't.

    The problem (as the message says) is that the character is illegal in URLs. It is perfectly fine in HTML.

    You need to apply encoding for URLs which would be %7C.