Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1918
  • Last Modified:

html_encode

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
srinusimhadri
Asked:
srinusimhadri
  • 4
  • 4
  • 3
1 Solution
 
Ernest022699Commented:
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
 
srinusimhadriAuthor Commented:
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
 
Ernest022699Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
a.marshCommented:
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
 
Ernest022699Commented:
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
 
srinusimhadriAuthor Commented:
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
 
a.marshCommented:
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
 
srinusimhadriAuthor Commented:
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
 
a.marshCommented:
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
 
srinusimhadriAuthor Commented:
Great, Thats what I wanted.

Thank U a.marsh


0
 
a.marshCommented:
Glad to have helped. :o)

Ant
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now