C++ HTML Encode Function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

C++ HTML Encode Function

 
0
  #1
Jan 4th, 2007
looking for a function that will htmlencode a cstring
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: C++ HTML Encode Function

 
0
  #2
Jan 4th, 2007
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:

  1. string htmlcode = "<p>"+paragraph+"</p>";
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
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,056
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: C++ HTML Encode Function

 
0
  #3
Jan 4th, 2007
To HTML encode a string is to convert the " to &quot; and < to &lt; and > to &gt; and & to &amp; and you get the idea 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: C++ HTML Encode Function

 
0
  #4
Jan 4th, 2007
Originally Posted by cscgal View Post
To HTML encode a string is to convert the " to &quot; and < to &lt; and > to &gt; and & to &amp; and you get the idea Essentially it's just a simple find and replace of those few things.
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,124
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C++ HTML Encode Function

 
0
  #5
Jan 4th, 2007
Originally Posted by campkev View Post
...was just looking for a function that was already written and handled all of them rather than doing them each individually....
Nope, sorry. You have to be efficient on your own...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: C++ HTML Encode Function

 
0
  #6
Jan 5th, 2007
Originally Posted by joeprogrammer View Post
[edit]Or did you mean something like this... [/edit]
that's what I am looking for, but a non-.NET version
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: C++ HTML Encode Function

 
0
  #7
Jan 5th, 2007
Originally Posted by campkev View Post
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.
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. #define array_length(array) (sizeof (array) / sizeof (array)[0])
  6.  
  7. namespace Raye {
  8. using namespace std;
  9.  
  10. struct HTMLReplace {
  11. string match;
  12. string replace;
  13. } codes[] = {
  14. {"&", "&amp;"},
  15. {"<", "&lt;"},
  16. {">", "&gt;"}
  17. };
  18.  
  19. string HTMLEncode( const string& s )
  20. {
  21. string rs = s;
  22.  
  23. // Replace each matching token in turn
  24. for ( size_t i = 0; i < array_length( codes ); i++ ) {
  25. // Find the first match
  26. const string& match = codes[i].match;
  27. const string& repl = codes[i].replace;
  28. string::size_type start = rs.find_first_of( match );
  29.  
  30. // Replace all matches
  31. while ( start != string::npos ) {
  32. rs.replace( start, match.size(), repl );
  33. // Be sure to jump forward by the replacement length
  34. start = rs.find_first_of( match, start + repl.size() );
  35. }
  36. }
  37.  
  38. return rs;
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. using namespace std;
  45.  
  46. cout << Raye::HTMLEncode( "template <class T> void foo( const string& bar );" ) << '\n';
  47.  
  48. return 0;
  49. }
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.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: C++ HTML Encode Function

 
0
  #8
Jan 5th, 2007
Originally Posted by Ravalon View Post
I'm sure you could find something with google, but it's not hard to write your own either.
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. #define array_length(array) (sizeof (array) / sizeof (array)[0])
  6.  
  7. namespace Raye {
  8. using namespace std;
  9.  
  10. struct HTMLReplace {
  11. string match;
  12. string replace;
  13. } codes[] = {
  14. {"&", "&amp;"},
  15. {"<", "&lt;"},
  16. {">", "&gt;"}
  17. };
  18.  
  19. string HTMLEncode( const string& s )
  20. {
  21. string rs = s;
  22.  
  23. // Replace each matching token in turn
  24. for ( size_t i = 0; i < array_length( codes ); i++ ) {
  25. // Find the first match
  26. const string& match = codes[i].match;
  27. const string& repl = codes[i].replace;
  28. string::size_type start = rs.find_first_of( match );
  29.  
  30. // Replace all matches
  31. while ( start != string::npos ) {
  32. rs.replace( start, match.size(), repl );
  33. // Be sure to jump forward by the replacement length
  34. start = rs.find_first_of( match, start + repl.size() );
  35. }
  36. }
  37.  
  38. return rs;
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. using namespace std;
  45.  
  46. cout << Raye::HTMLEncode( "template <class T> void foo( const string& bar );" ) << '\n';
  47.  
  48. return 0;
  49. }
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.
thanks, i'll try this.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 2
Reputation: King.Kong is an unknown quantity at this point 
Solved Threads: 0
King.Kong King.Kong is offline Offline
Newbie Poster

Re: C++ HTML Encode Function

 
0
  #9
Jan 19th, 2007
Not so fast. You have to take care of a long list of other entities too, like &amp;pound; for &pound;, etc. See here.

And if the & symbol is already part of an encoded literal, you don't want to encode it one more time.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 2
Reputation: King.Kong is an unknown quantity at this point 
Solved Threads: 0
King.Kong King.Kong is offline Offline
Newbie Poster

Re: C++ HTML Encode Function

 
0
  #10
Jan 19th, 2007
And all Unicode characters that cannot be represented within 8 bits have to be encoded too to &#xnnnn;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 7301 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC