473,216 Members | 1,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

Encoding & Decoding text

FP
I have a javascript variable set to the contents of a database comments
field.
To set the js variable I used the PHP addslashes function which encodes
the apostrophe, double quotes and the backslash.
I need to use the variable contents 3 times;
- display on HTML page
- pre-populate a text area with it
- make it the body of an e-mail

When I display the variable using document.write, things like "<b>"
turn the text to bold. To get around this problem I found the function
function htmlEncode(s){
return s.replace(/&(?!\w+([;\s]|$))/g, "&amp;").replace(/</g,
"&lt;").replace(/>/g, "&gt;");}

However, this function turns my original single and double quotes into
"&amp..."
My thought now is I need to convert the single & double quotes back to
their original characters, then run it through the above function.

Is there an easier way to do this?
If not, how do I convert the "&amp..." back to the real character
before passing it to the above function?
As far as I can tell the function works, does anyone see problems with
it?

Jul 4 '06 #1
2 2705
FP wrote:
I have a javascript variable set to the contents of a database comments
field.
To set the js variable I used the PHP addslashes function which encodes
the apostrophe, double quotes and the backslash.
That isn't enough. The better way is to convert all suspicious
characters to (numeric or not) HTML entities in PHP, before it reaches
client script. I'ld say the PHP regex should at least affect < " &
and \.

PHP has pre-built constructions for this kind of things.
I need to use the variable contents 3 times;
- display on HTML page
- pre-populate a text area with it
- make it the body of an e-mail
Yes, those things know how to handle HTML entities (don't forget to set
your email's Content-Type to 'text/html' and its MIME-Version >= 1).
When I display the variable using document.write, things like "<b>"
turn the text to bold. To get around this problem I found the function
function htmlEncode(s){
return s.replace(/&(?!\w+([;\s]|$))/g, "&amp;").replace(/</g,
"&lt;").replace(/>/g, "&gt;");}

However, this function turns my original single and double quotes into
"&amp..."
My thought now is I need to convert the single & double quotes back to
their original characters, then run it through the above function.

Is there an easier way to do this?
If not, how do I convert the "&amp..." back to the real character
before passing it to the above function?
It should be done at PHP level; there's actually no need for javascript
trickery here.

--
Bart

Jul 5 '06 #2
FP
Bart Van der Donck wrote:
FP wrote:
I have a javascript variable set to the contents of a database comments
field.
To set the js variable I used the PHP addslashes function which encodes
the apostrophe, double quotes and the backslash.

That isn't enough. The better way is to convert all suspicious
characters to (numeric or not) HTML entities in PHP, before it reaches
client script. I'ld say the PHP regex should at least affect < " &
and \.
I checked for a regex() function but couldn't find one. The closest
thing I found was the htmlspecialchars() function which converts the
characters you mentioned.
I need to use the variable contents 3 times;
- display on HTML page
- pre-populate a text area with it
- make it the body of an e-mail

Yes, those things know how to handle HTML entities (don't forget to set
your email's Content-Type to 'text/html' and its MIME-Version >= 1).
This is going to sound stupid but how do I do that. Is this part of an
href?
I'm using javascript code because that's the only way I know to make a
button in HTML the code I'm using is;

function Email(TheText){
document.location.href=("mailto:?body=" + TheText);
}

<TD>
<input type="button" onclick="Email('<? addslashes(PHP Result here)
?>')" value="E-mail"
</TD>

The PHP Results are the contents of a comment field.
When I click the e-mail button I want the comment in the body of my
e-mail, nothing else needs to be filled in.
If there's an ampersand in the comments then all data after the
ampersand is lost.
The PHP addslashes() function adds ampersands when converting
apostrophes.
I tried putting the "E-mail" button in a form and having the PHP result
as a hidden field hoping I didn't have to convert the apostrophe's in
that case but then the E-mail button didn't line up with the 2 buttons
before it so I gave up on that idea.

Obviously I'm going about this the wrong way, what's the right way?

Jul 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dot | last post by:
Hi all, I have written a Python huffman Encoding Module, for my own amusement. I thought it might be educational/entertaining for other people, so I've put it on my website and wrote about it a...
8
by: timtos | last post by:
I want to save text in a file and after that I want to display this textfile using the internet explorer. If I am displaying "html text" everything is fine but if I want to display plain text...
0
by: mubx2000 | last post by:
Hi , I'm looking for code (Symbian C++) that can do the following things: 1-Analyzing the (MIME) types (Content-typemContent-transfere-encoding,Mime Version,Conent Description). 2-Download...
1
by: matt | last post by:
hello, i need to do some HTML encoding on some strings. not UrlEncoding, mind you, but HTML encoding. ie: "&" to "&amp;" "<" to "&lt;" ....etc. does anyone know of a good way to do this, other...
14
by: Zoro | last post by:
My task is to read html files from disk and save them onto SQL Server database field. I have created an nvarchar(max) field to hold them. The problem is that some characters, particularly html...
0
by: AndrewJ | last post by:
I am having problem decoding data which is imported into a MySQL 4.0.* DB (thus no UTF-8 character set encoding possible) Basically my application utilises PEAR for DB abstraction. I import...
9
by: KWSW | last post by:
Having settled the huffman encoding/decoding and channel modeling(thanks to the previous part on bitwise operation), the last part would be hamming encoding/decoding. Did some research as usual on...
3
by: =?Utf-8?B?cndvb2RydWY=?= | last post by:
Hello All, I am using an HtmlTextWriter to writer out some html. Prior to sending the content to the text writer, HttpUtility.HtmlEncode the string. However, doing so results in a string where...
0
by: Michele | last post by:
Hi there, I'm using a python script in conjunction with a JPype, to run java classes. So, here's the code: from jpype import * import os import random import math import sys
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.