| | |
C++ HTML Encode Function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
What do you mean by that? Simply put <p> tags around a string? If that's what you want, you can write your own very simply:
Likewise, searching and replacing newlines with <br/> is also very easy. Look up some of the string functions like string::find() and string::replace():
http://www.bgsu.edu/departments/comp...cs/string.html
[edit]Or did you mean something like this...
[/edit]
Hope this helps
C++ Syntax (Toggle Plain Text)
string htmlcode = "<p>"+paragraph+"</p>";
http://www.bgsu.edu/departments/comp...cs/string.html
[edit]Or did you mean something like this...
[/edit]Hope this helps
Last edited by John A; Jan 4th, 2007 at 8:23 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
To HTML encode a string is to convert the " to " and < to < and > to > and & to & and you get the idea
Essentially it's just a simple find and replace of those few things.
Essentially it's just a simple find and replace of those few things. Last edited by cscgal; Jan 4th, 2007 at 8:38 pm.
Dani the Computer Science Gal 
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
yeah, i know, was just looking for a function that was already written and handled all of them rather than doing them each individually. trying to do it the most laz.. I mean efficient way and use an existing function rather than write my own
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
that's what I am looking for, but a non-.NET version
I'm sure you could find something with google, but it's not hard to write your own either.
Just add to the array when you want to handle another encoding, and be careful about encodings that are order sensitive. For example, the & encoding has to be done first because the others use & in the result.
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <iostream> #include <string> #define array_length(array) (sizeof (array) / sizeof (array)[0]) namespace Raye { using namespace std; struct HTMLReplace { string match; string replace; } codes[] = { {"&", "&"}, {"<", "<"}, {">", ">"} }; string HTMLEncode( const string& s ) { string rs = s; // Replace each matching token in turn for ( size_t i = 0; i < array_length( codes ); i++ ) { // Find the first match const string& match = codes[i].match; const string& repl = codes[i].replace; string::size_type start = rs.find_first_of( match ); // Replace all matches while ( start != string::npos ) { rs.replace( start, match.size(), repl ); // Be sure to jump forward by the replacement length start = rs.find_first_of( match, start + repl.size() ); } } return rs; } } int main() { using namespace std; cout << Raye::HTMLEncode( "template <class T> void foo( const string& bar );" ) << '\n'; return 0; }
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
•
•
•
•
I'm sure you could find something with google, but it's not hard to write your own either.
Just add to the array when you want to handle another encoding, and be careful about encodings that are order sensitive. For example, the & encoding has to be done first because the others use & in the result.C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <iostream> #include <string> #define array_length(array) (sizeof (array) / sizeof (array)[0]) namespace Raye { using namespace std; struct HTMLReplace { string match; string replace; } codes[] = { {"&", "&"}, {"<", "<"}, {">", ">"} }; string HTMLEncode( const string& s ) { string rs = s; // Replace each matching token in turn for ( size_t i = 0; i < array_length( codes ); i++ ) { // Find the first match const string& match = codes[i].match; const string& repl = codes[i].replace; string::size_type start = rs.find_first_of( match ); // Replace all matches while ( start != string::npos ) { rs.replace( start, match.size(), repl ); // Be sure to jump forward by the replacement length start = rs.find_first_of( match, start + repl.size() ); } } return rs; } } int main() { using namespace std; cout << Raye::HTMLEncode( "template <class T> void foo( const string& bar );" ) << '\n'; return 0; }
•
•
Join Date: Jan 2007
Posts: 2
Reputation:
Solved Threads: 0
Not so fast. You have to take care of a long list of other entities too, like &pound; for £, etc. See here.
And if the & symbol is already part of an encoded literal, you don't want to encode it one more time.
And if the & symbol is already part of an encoded literal, you don't want to encode it one more time.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Grade Program
- Next Thread: need help in c++ ,can't stop cursor blink
Views: 7301 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






