looking for a function that will htmlencode a cstring

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:

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/compsci/docs/string.html

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

Hope this helps

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.

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

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

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

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

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

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.

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.

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.