Solved

html_encode

Posted on 2001-07-23
11
1,909 Views
Last Modified: 2007-11-27
Hi all,
 Hope this interests you.
I am creating an html report for the data from DB.
I wrap the data along with html to get the format.
But, what I need to make sure is that the data that
comes from db is intact like spaces,<,> etc...

So, I want to first encode the data from the DB to make it
html safe, like converting spaces into &nbsp etc...

But I dont have a built-in function for this. So,
I need to write the program. Can anybody provide the
logic for the same.(like what char are to be
turned to what chars)

Thanks in Advance.

PS:- If any body knows the equivalent function that is
available in oracle, please advise.

0
Comment
Question by:srinusimhadri
  • 4
  • 4
  • 3
11 Comments
 
LVL 1

Expert Comment

by:Ernest022699
ID: 6310871
Don't change all spaces into &nbsp; !

The first question is: What characters are in the database?  Do you have control over what goes in?

Second: Any particular language you'd like used?  Perl would be fairly easy.
0
 

Author Comment

by:srinusimhadri
ID: 6310930
let me give an example.

1. I have an sql query like this.

   select '<tr><td>'||ename||'</td><td>'||remarks||'</td></tr>' from emp;

2. In the above example I am combining the values from the db with html. the column 'Remarks', could contain any type of characters, like special characters >,<,;,:, spaces etc.

3. Because the html does'nt recognise space as a space data like
'this   is  remarks with lot       of       spaces'
would be shown as
'this is remarks with lot of spaces'

4. And same problems with >, < and some other symbols which I dont know.

5. I dont want to make use of <pre> tag.

6. I may be using pl/sql to write that function.


0
 
LVL 1

Expert Comment

by:Ernest022699
ID: 6310995
Sorry, I can't help with SQL.  The rest of this comment contains hints which you should consider no matter how you do the conversion.

The <pre> tag would be idea for what you want to do.  Why don't you want to use it?

Your first step MUST BE to find all characters in the database which would provide problems when mixed with HTML.  This is not as complex as it sounds.  Any decent HTML book will give you the list AND their numeric equivalents.  (Don't forget to convert the TAB character to a space.)

I've got no sympathy for wanting to NOT collapse multiple spaces  to a single space.  Please sometimes use multiple spaces to align text.  This has no place in HTML.

Good luck.
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6312342
You're looking to do this encoding from within the database then?

Surely you are using some kind of scripting language (or equivalent) to take the data from the database and display it in the browser?

Ant
0
 
LVL 1

Expert Comment

by:Ernest022699
ID: 6312988
Blame the typos in my earlier comment on the late hour.
"idea" should be "ideal".
"Please" should be "People".

a.marsh asks a good question.  Do you want to fix the data as it is entered into the database or when it is retrieved?  If the database is to be used for things other than generating HTML then you probably just want to leave it alone in the database and convert it solely when generating HTML.

Knowing which language you're going to use is important.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:srinusimhadri
ID: 6342895
Ernest,
  I know that I have to find the characters which will give problems from the data of the database.
I know the logic. Its a very very small program.

MY QUESTION IS,
    what are those characters???
 
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6343600
Well if you answer my question srinusimhadri then you may not have to worry about what the characters are!!!!!

For example, if you are using PHP there are two built-in functions that will do it all for you:

htmlentities()

and

htmlspecialchars()


So now my question is are you using PHP??

:o)

Ant
0
 

Author Comment

by:srinusimhadri
ID: 6343639
No, I am going to use PL/SQL to do this.
PL/SQL is my serverside scripting lang.
Anyway,now, I came to know that such function is there(previously, I thought the same is not available which made me to post this question).

But that function encodes all characters and collapses the space characters. I want to have each &nbsp for each space in my data.

So, I want to write my own function still.

So, What are those characters.(please a.marsh 8-} )

0
 
LVL 15

Accepted Solution

by:
a.marsh earned 50 total points
ID: 6346704
Well you have some choices - you can either convert all special characters or simply convert the ones that could possibly cause problems (hence the two different functions in PHP). I personally would go for the latter.

You can get a complete list of special characters here:

http://utopia.knoware.nl/users/schluter/doc/tags/characters.html

I personally would suggest only worrying about converting:

> - &gt;
< - &lt;
& - &amp;
*space* - &nbsp;



:o)

Ant
0
 

Author Comment

by:srinusimhadri
ID: 6346939
Great, Thats what I wanted.

Thank U a.marsh


0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6347828
Glad to have helped. :o)

Ant
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now