Link I" /> Link I" /> Link I"/>

Proper Way to Escape the | Character Using HTML Entities

245 Views Asked by At

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."

2

There are 2 best solutions below

2
Quentin On BEST ANSWER

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.

3
Dharman On

I don't know why tidy is complaining about it, but this character is not problematic in HTML nor in URL. | is not a reserved character and can be used in URL as is. You can percent-encode every character, but there is really no need for it.

What I would presume Tidy might be complaining is =. You have got two of them, the second being an invalid one.

There is no need to encode this character in HTML entities. It has no special meaning in HTML.