kitenski
kitenski

Reputation: 639

Ampersand in link description text?

I am validating my site using http://validator.w3.org and have an issue where there is an & in my link description text.

This is taken from the source:

<a class='tag' href='/php/iphone software.php?function=developer&amp;dev=witch%26wizards+inc.&amp;store=143441'>witch&wizards inc.</a>

This gives this error in the validator:

Line 188, Column 540: cannot generate system identifier for general entity "wizards"

…6wizards+inc.&store=143441'>witch&wizards inc.

✉ An entity reference was found in the document, but there is no reference by that name defined

If I urlencode the description then the validation passes, but the user then sees the text displayed urlencoded, ie

Developer witch%26wizards+inc.

However, I believe it's much more user friendly if this was displayed unencoded, ie

Developer witch&wizards inc.

Is there a way to pass validation, but still have user friendly text displayed?

Upvotes: 4

Views: 4454

Answers (3)

moinudin
moinudin

Reputation: 138347

Encode & in the URL as %26, not as &amp; (also encode the space as %20) and encode the & in the displayed text as &amp;:

<a class='tag' href='/php/iphone%20software.php?function=developer%26dev=witch%26wizards+inc.%26store=143441'>witch&amp;wizards inc.</a>

Note that there is a difference between URL encoding which are used to encode URLS and HTML entities used to encode displayed text in HTML. For example, if you wanted to display the text <br/> in HTML you would encode it as &lt;br/&gt;, but in a URL it would be %3Cbr%2F%3E.

Use htmlentities() to encode special characters as HTML entities in PHP.

Upvotes: 1

BoltClock
BoltClock

Reputation: 723598

Simple:

For ampersands as part of query string values, URL encode them to %26.

For displaying ampersands as text, or ampersands used to separate query string key-value pairs — in other words, for almost everything else — HTML encode them to their entities &amp;.

Your HTML should look like this:

<a class='tag' href='/php/iphone%20software.php?function=developer&amp;dev=witch%26wizards+inc.&amp;store=143441'>witch&amp;wizards inc.</a>

Upvotes: 9

Fahad Sadah
Fahad Sadah

Reputation: 2418

Try using &amp; rather than %26

EDIT: woah, &amp; is expanded on SO.

Upvotes: 2

Related Questions