473,378 Members | 1,457 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,378 software developers and data experts.

How to encode text into html format

Hi,

I want to encode input text into html format such as replace "<" with "&lt",
replace "&" with "&amp".
Could you give me some ideas? Thanks.

Fred


Jun 27 '08 #1
10 2904
Sam
Fred Yu writes:
Hi,

I want to encode input text into html format such as replace "<" with "&lt",
That's "&lt;".
replace "&" with "&amp".
That's "&amp;".
Could you give me some ideas? Thanks.
Try to do your homework assignment by yourself. This is a simple
search/replace operation, and there are many ways to get it done.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhC4aQACgkQx9p3GYHlUOJbHQCdFkadJpXTIS tsphfd9cZ1yjhc
4QwAniVg+D//DKToDNz651FJF5MCYIlo
=xhO4
-----END PGP SIGNATURE-----

Jun 27 '08 #2
Fred Yu wrote:
Hi,

I want to encode input text into html format such as replace "<" with
"&lt", replace "&" with "&amp".
Could you give me some ideas? Thanks.

Containers: std::map< char, std::string >
Iterators: std::istream_iterator, std::ostream_iterator
Algorithms: std::transform
Best

Kai-Uwe Bux
Jun 27 '08 #3
On Jun 1, 12:37*pm, "Fred Yu" <jean_y_f...@sohu.comwrote:
Hi,

I want to encode input text into html format such as replace "<" with "&lt",
replace "&" with "&amp".
Could you give me some ideas? Thanks.

Fred
google iconv. It will convert from many char encodings to many other
char
encodings. I've used it to "format" text in various XML wrapper
classes.
Jun 27 '08 #4

"Fred Yu" <je*********@sohu.comschrieb im Newsbeitrag
news:g1**********@news.cn99.com...
Hi,

I want to encode input text into html format such as replace "<" with
"&lt",
replace "&" with "&amp".
Example for AnsiString Class

AnsiString Input; //contains the html code
int pos;

do // replace "<" to "&lt"
{
if(Input.Pos("<") NULL)
{
pos = Input.Pos("<");
Input.Delete(pos,1);
Input.Insert("%26lt",pos);
}
}
while(Input.Pos("<") NULL);

Jun 27 '08 #5
On Jun 1, 8:11 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Fred Yu wrote:
I want to encode input text into html format such as replace "<" with
"&lt", replace "&" with "&amp".
Could you give me some ideas? Thanks.
Containers: std::map< char, std::string >
Iterators: std::istream_iterator, std::ostream_iterator
Algorithms: std::transform
Agreed for the first (although it may be overkill---in this
particular case, I think I'd go with a simple switch).

No real need for the second; just use istream::get() and
ostream::put() (or operator<< in some cases).

As to the third: how? You're replacing a single character with
a sequence of characters, and transform does a one to one (which
in practice makes it of fairly limited utility---although I've
used it with a vector<string>, ostream_iterator, and as string
transformer class that I've written, which works something like
$(patsubst...) in GNU make).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6
James Kanze wrote:
On Jun 1, 8:11 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>Fred Yu wrote:
I want to encode input text into html format such as replace "<" with
"&lt", replace "&" with "&amp".
Could you give me some ideas? Thanks.
>Containers: std::map< char, std::string >
Iterators: std::istream_iterator, std::ostream_iterator
Algorithms: std::transform

Agreed for the first (although it may be overkill---in this
particular case, I think I'd go with a simple switch).

No real need for the second; just use istream::get() and
ostream::put() (or operator<< in some cases).

As to the third: how? You're replacing a single character with
a sequence of characters, and transform does a one to one (which
in practice makes it of fairly limited utility---although I've
used it with a vector<string>, ostream_iterator, and as string
transformer class that I've written, which works something like
$(patsubst...) in GNU make).
I was thinking of something like this:

#include <iostream>
#include <iterator>
#include <map>
#include <algorithm>
#include <cassert>

struct encoder {

std::map< char, std::string the_map;

encoder ( void ) {
the_map[ 'a' ] = "a";
// ...
the_map[ '&' ] = "&amp";
// ...
}

std::string const & operator() ( char ch ) const {
std::map< char, std::string >::const_iterator iter =
the_map.find( ch );
assert( iter != the_map.end() );
return ( iter->second );
}
};

int main ( void ) {
encoder the_encoder;
std::transform( std::istreambuf_iterator<char>( std::cin ),
std::istreambuf_iterator<char>(),
std::ostream_iterator<std::string>( std::cout, "" ),
the_encoder );
}
Best

Kai-Uwe Bux

Jun 27 '08 #7
Hi!

James Kanze schrieb:
As to the third: how? You're replacing a single character with
a sequence of characters, and transform does a one to one (which
in practice makes it of fairly limited utility---although I've
used it with a vector<string>, ostream_iterator, and as string
transformer class that I've written, which works something like
$(patsubst...) in GNU make).
The source range of transform may have another value type than the
destination range.

char const* replace(char);

transform(str.begin(), str.end(),
ostream_iterator<const char*>(cout),
&replace);

Frank
Jun 27 '08 #8
On Jun 1, 11:25 pm, Frank Birbacher <bloodymir.c...@gmx.netwrote:
James Kanze schrieb:
As to the third: how? You're replacing a single character with
a sequence of characters, and transform does a one to one (which
in practice makes it of fairly limited utility---although I've
used it with a vector<string>, ostream_iterator, and as string
transformer class that I've written, which works something like
$(patsubst...) in GNU make).
The source range of transform may have another value type than the
destination range.
I'm aware of that, however...
char const* replace(char);
transform(str.begin(), str.end(),
ostream_iterator<const char*>(cout),
&replace);
For some reason, I was thinking in terms of std::string, and not
char const*. And converting each std::string seemed a bit heavy
for the task at hand. But a statically generated char const*[];
why not?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #9
Hi!

James Kanze schrieb:
>char const* replace(char);
>transform(str.begin(), str.end(),
ostream_iterator<const char*>(cout),
&replace);

For some reason, I was thinking in terms of std::string, and not
char const*. And converting each std::string seemed a bit heavy
for the task at hand. But a statically generated char const*[];
why not?
Yes. I think I needed such a conversion once and used a switch. The
obvious problem is to efficiently handle a char that is not transformed
to more than one char (the common case). I think I actually used
for_each instead of transform:

void appendReplacement(ostream& stream, const char c)
{
switch(c)
{
case '<': stream << "&lt;"; break;
default: stream << c; break;
}
}

This makes it possible to append different types (char or char*) to the
stream and yet requires no [CHAR_MAX] array, but lets the compiler
choose the most efficient lookup (through the switch).

Of course can this function be implemented as a functor.

Frank
Jun 27 '08 #10

"Fred Yu" <je*********@sohu.comдÈëÏûÏ¢ÐÂÎÅ:g1**********@new s.cn99.com...
Hi,

I want to encode input text into html format such as replace "<" with
"&lt",
replace "&" with "&amp".
Could you give me some ideas? Thanks.

Fred

Thanks for your help.
Jun 27 '08 #11

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

Similar topics

4
by: francescomoi | last post by:
Hi. I'm trying to store a text within a MySQL field (v 3.23.58) by using MySQLdb (v 1.2.1c3). The text is: "telephone..." (note the last character) And I get this error message:...
3
by: user | last post by:
hi there has anyone of you writte a function to encode html from like '&' -> '&amp;' and likes to share it with me.. or can anybody give me a hint how to set up something like that. cheers me. ...
5
by: Scott Matthews | last post by:
I've recently come upon an odd Javascript (and/or browser) behavior, and after hunting around the Web I still can't seem to find an answer. Specifically, I have noticed that the Javascript...
4
by: Newbie | last post by:
How would I modify this form to encode *all* the characters in the 'source' textarea to the '%xx' format & place result code into the 'output' textarea? (cross browser compatable) Any help is...
2
by: ViperDK | last post by:
What is the best way for that? I store all Data in the original form in the Database. To prevent output fields (especially the fields everyone can use) to do bad things like killing the...
4
by: Darrel | last post by:
How does HTML.encode work? I'm trying to save text in a hidden form field into a SQL DB. The tedt is HTML (from a WYSIWYG editor...X-standard). One problem I have is that stray apostrophe's in...
7
by: jtfaulk | last post by:
I need to encode some information on the server side using ASP.NET with C#; sending via HTTP to a client side application, that needs to be decoded in an MFC C++ application. I'm not sure if I...
0
by: younus | last post by:
Hi Guys I need some help i want to encode arabic text to UCS2 format in asp my text بيسليسلسيس is converted to 0633062C064A06440643002006280646062C0627 this format plese help me this is very...
0
by: younus | last post by:
Hi Guys I need some help i want to encode arabic text to UCS2 format in asp my text بيسليسلسيس is converted to 0633062C064A06440643002006280646062C0627 this format plese help me this is very...
9
by: Fred Yu | last post by:
Hi, I want to encode input text into html format such as replace "<" with "&lt", replace "&" with "&amp". Could you give me some ideas? Thanks. Fred
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.