473,396 Members | 2,129 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,396 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 2713
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.