954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ HTML Encode Function

looking for a function that will htmlencode a cstring

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

What do you mean by that? Simply put tags around a string? If that's what you want, you can write your own very simply:

string htmlcode = "<p>"+paragraph+"</p>";



Likewise, searching and replacing newlines with
is also very easy. Look up some of the string functions like string::find() and string::replace(): http://www.bgsu.edu/departments/compsci/docs/string.html

[edit]Or did you mean something like this ... ;)[/edit]

Hope this helps

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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.

cscgal
The Queen of DaniWeb
Administrator
19,424 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 
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.


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

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 
...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... ;)

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
[edit]Or did you mean something like this ... ;)[/edit]

that's what I am looking for, but a non-.NET version

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
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.

#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[] = {
    {"&", "&amp;"},
    {"<", "&lt;"}, 
    {">", "&gt;"}
  };

  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;
}

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. ;)

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 

I'm sure you could find something with google, but it's not hard to write your own either.

#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[] = {
    {"&", "&amp;"},
    {"<", "&lt;"}, 
    {">", "&gt;"}
  };

  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;
}

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.

campkev
Posting Pro in Training
484 posts since Jul 2005
Reputation Points: 14
Solved Threads: 19
 

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.

King.Kong
Newbie Poster
2 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

And all Unicode characters that cannot be represented within 8 bits have to be encoded too to nnnn;

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

hmm, except if the string represents PCDATA in which case it should be surrounded by tags.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You