Is there a way to prevent ampersand warnings

Is there a way to prevent ampersand warnings (which I think slow down the page display)?

This a typical example and produces a warning:

M&M’s are lovely

but this does not:

M&M’s are lovely

I could go through the data and replace each and every occurrence it is just that there are too many.

There is a reason for & but that’s another discussion.
Load your file into notepad and use “ctrl H” Find what: & Replace with: & then click Replace all.
Should work well unless you have other entities that use the ampersand. you wouldn’t want “&lt;” in place of “<” If so you would want to use “Find Next” then either “Replace” or “Find Next” as necessary. This will take a little longer.
hope this helps.

Many thanks for your suggestion unfortunately the data is in a table (link below). There are over 1,500 titles which are hidden by JavasScript but do produce warnings and slow the page display.

I will write a PHP script which replaces the ampersands and hope it does not cause any other problems.

I was hoping for another solution…


Just ignore the warnings. There is nothing you can do about that unless you want to go into all your files.

You could write the PHP script as you say, however that’s be a little stupid to do all this over a little warning :slight_smile:

Yes. Escape every ampersand as &amp;. The ampersand has special meaning in SGML/HTML/XML and must be escaped in most circumstances.

I don’t understand why this thread is in the CSS forum, though, since it doesn’t have anything whatsoever to do with CSS. :confused:

Really, really bad advice! :mad:
I’m surprised and disappointed in you, Ryan! :nono:

I have moved this to the HTML forum since the question is about invalid HTML entity codes such as &M; where &M was what was intended.

  1. I’m no HTML/semantics/whatever you wanna call this…guru. I’m a CSS guy. I don’t mess with HTML (this sorta stuff) so…I don’t always give good advice in this subject… :p.
  2. I wrote that after literally 3 hours of sleep…I forgot I had an 8 A.M. college class :frowning:

I take the same logic for CSS warnings Tommy, so I just thought of giving that advice.

Ryan, here’s a good rule of thumb to remember: Garbage In, Garbage Out.

If you have syntax errors in your markup, there are no guarantees. Error handling is unspecified in HTML4. And if there’s a presentational problem, how do you know whether the fault is in your CSS or in your invalid markup?

I personally would know what the problem is just because I know I would lol :). I can’t explain it

I haven’t encountered an error caused by HTML warnings (not to say I won’t ever get them)

There are two types of fields that have special meanings in HTML.

Those that start with < are HTML tags. Those that start with & are HTML entities.

If you want those actual characters to display you need to use the HTML entity codes for them which are < and & respectively.

If what you enter after an & isn’t a valid entity code you get a warning message and the the code itself displays in the page instead of what it is supposed to represent.

For an example of what can happen if you forget to use the entity code consider:


which will not display the same thing as


since &amp and &copy are two different valid entity codes.

O, I didn’t expect you to say this, so maybe I’ve misinterpreted you in the past. I thought you were against using encodings (at least when using UTF-8).

I realize that &amp; is needed in URIs, but thought it would generally be safe in normal text.

Tommy way referring to escaping characters which can actually be included in text using UTF-8. As Stephen wrote, < and & have special meaning in HTML (as does " in attributes), so they must always be escaped, no matter what. Not escaping them is like having a byte with only two bits - your machine simply can’t understand it, no matter how hard it tries.

Stephen also gave an excellent example of why it is not safe to include it in text.

As for the original question: It should be trivial to do a mass search-replace. If you have the pages as individual files, there are freeware programs which can do multiple-file search-replace - some even with RegEx-support (I once converted an entire site from ASPX to PHP using only search-replace commands ;)). If you have the info in a database, it’s even easier.

Yes, it’s clearly fraught with danger not to escape the ampersand. Personally I always do—and a lot else, besides, just to be sure—but perhaps I took Tommy too literally about rarely using entity references etc.

I am. I don’t think you should use entity references for characters that can be expressed literally within the current encoding. Thus, ‘ä’, not &auml;, etc., unless you’re stuck with ISO 646 (US-ASCII) for some reason.

There are two exceptions from this rule:

[list=1][]Characters with special meaning, like ‘<’ and ‘&’, which must be escaped in most cases
]Characters that are invisible or impossible to tell from similar characters, like &nbsp; (non- break space) and &#173; (soft hyphen).[/list]

In fact, if I’m escaping ‘<’ characters, e.g., in a markup example, I’ll usually escape the ‘>’ characters as well, for symmetry. So sue me! :slight_smile:

The writ is in the mail. :wink:

Thanks for the clarification. You didn’t mention ampersand in the link I mentioned above, so I thought I’d check.

I have to admit, I find encodings quite fun, and use them even when I shouldn’t. Ah well, counter sue me! :lol:

I wouldn’t recommend doing a total search and replace on only &

It will take a bit more effort to do but help save you from making mistakes if you do each separately eg.
replace &A with &amp;A match case etc. etc. through the alphabet.

I would try to log the errors and then replace them as you become aware of them. Using a regex search / replace sounds like a very good idea. Just make sure you get the regex right.

However you go about it it sounds like a project. But remember, anything repetitive and tedious is a good candidate for automation. Spend the time looking for a way other than manually editing everything and save yourself some time.

In most cases you could probably get away with doing it as just two replaces

replace & with &
replace &amp; with &

Ah, excellent. I guess I was stuck in additive and didn’t think of subtractive. :tup:

Neither would I. Especially if you use <script> or <style> tags where the content may contain ampersands.

If you’re using HTML (including, of course, pretend-XHTML), those elements have a content model of CDATA, which means entity references are not parsed. So using &amp; would be an error.

I am. I don’t think you should use entity references for characters that can be expressed literally within the current encoding. Thus, ‘ä’, not ä, etc., unless you’re stuck with ISO 646 (US-ASCII) for some reason.

I always use entity references, though not the named but the decimal.

Two reasons:
one, I can’t type most of even the commonest ones on my keyboard (not even é and ë and ó which are extremely common in Dutch). While sometimes I’ll do a copy-pasta from Wikipedia (if you go to edit a page, you get all the letters presented to you), I don’t like trusting it. Also, typing special characters in VI is harder for me and copy-paste from the GUI is still not possible (guess I’ll figure that out some day).

Two, when you have colleagues who insist in setting up a database holding international names and addresses in Latin-1/iso-8859-1 while you are (properly) using and saving-as UTF-8, anywhere you have an actual decimal character entity, the poor browser will display it correctly. Everything else becomes a ? or a box or whatever your browser uses for “unknown character”.

My HTML pages are absolutely filled to the brim with decimal entities (and occasionally hex esp in CSS). I could not live without them.

Most Dutch have a US-keyboard and set the settings of the OS to “Dutch”. I kept mine set to en-us because the OS was set to that, meaning even the dedicated key I have for € does nothing : (

(all above special characters were typed in using decimal entities… lawlz they are easy to memorise)