Solved

html_encode

Posted on 2001-07-23
11
1,913 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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
 

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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article discusses four methods for overlaying images in a container on a web page
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

624 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