944,150 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11328
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 4th, 2007
0

C++ HTML Encode Function

Expand Post »
looking for a function that will htmlencode a cstring
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Jan 4th, 2007
0

Re: C++ HTML Encode Function

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:

C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 4th, 2007
0

Re: C++ HTML Encode Function

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 163
The Queen of DaniWeb
cscgal is offline Offline
13,646 posts
since Feb 2002
Jan 4th, 2007
0

Re: C++ HTML Encode Function

Click to Expand / Collapse  Quote originally posted by cscgal ...
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
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Jan 4th, 2007
0

Re: C++ HTML Encode Function

Click to Expand / Collapse  Quote originally posted by campkev ...
...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...
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Jan 5th, 2007
0

Re: C++ HTML Encode Function

[edit]Or did you mean something like this... [/edit]
that's what I am looking for, but a non-.NET version
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Jan 5th, 2007
0

Re: C++ HTML Encode Function

Click to Expand / Collapse  Quote originally posted by campkev ...
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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Jan 5th, 2007
0

Re: C++ HTML Encode Function

Click to Expand / Collapse  Quote originally posted by Ravalon ...
I'm sure you could find something with google, but it's not hard to write your own either.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Jan 19th, 2007
0

Re: C++ HTML Encode Function

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
King.Kong is offline Offline
2 posts
since Jan 2007
Jan 19th, 2007
0

Re: C++ HTML Encode Function

And all Unicode characters that cannot be represented within 8 bits have to be encoded too to &#xnnnn;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
King.Kong is offline Offline
2 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Grade Program
Next Thread in C++ Forum Timeline: need help in c++ ,can't stop cursor blink





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC