Forum Moderators: open

Message Too Old, No Replies

replace all & with &

         

almo136

10:19 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



Hi,

I have a site with user submitted content. The problem is this content often contains &'s which don't validate.

Is it possible to automatically replace all of these with &

Thanks.

lavazza

11:03 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



I posted a client-side script at [webmasterworld.com...]

It's ugly... but it works and ought to be easily adapted to your needs, I hope

almo136

3:20 am on Feb 24, 2009 (gmt 0)

10+ Year Member



Hi lavazza,

Thanks for that. I had a look at it but a little over my head i'm afraid.

tangor

7:21 am on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Some html ready editors have an extended search and replace function that will search open docs, or all docs in folder, or all docs in folder and subfolders. Some of those editors are free. This, of course, is after the fact. What you want is to catch it at user entry.

HOWEVER be very careful changing & to & if your page/code contains links using that character.

daveginorge

8:00 am on Feb 24, 2009 (gmt 0)

10+ Year Member



One of the best editors I have found for short work is Notepad++ [notepad-plus.sourceforge.net ] works with just about every programming language around. It also handles the UNIX end of line character without problems unlike the MS notepad. I always install it as the default notepad program on PC's

When doing a "GLOBAL replace" (replace all) remember that all & will get replaced so   will become &ampnbsp; also existing & will become &ampamp; if you are not very careful.

I would go for the automated option as anything else will involve a lot of webmaster interactivity to keep the code validating. The code provided by "lavazza" is a good starting place for your first Javascript lesson, a few days spent learning the basics of Javascript can only help. [w3schools.com ]. The code only looks daunting because of the long list of values to be converted, those can be just cut and pasted as they will always be the same.

I would like to thank "lavazza", I have tagged this code for future use.

Dave

penders

2:10 pm on Feb 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a site with user submitted content. The problem is this content often contains &'s which don't validate.

(Sorry, I realise we're in the HTML forum, but...) Presumably your user submitted content is being processed server-side? If using PHP, for instance, you can call a function like htmlspecialchars() [uk.php.net] prior to outputting your content which (as its name suggests) converts special characters (including ">", "<" etc.) into to their html entities. Not only good for validation, but also important for security.

A JS solution is not really going to help with validation, or security, since the invalid characters will have already been downloaded to the client before they can be processed. A validator does not generally see the code/page post JavaScript.