http://forums.codeguru.com/showthread.php?448809-C-Replacing-HTML-Character-Entities
Ok pozamieniałem w pętli znaki w stringach " i ' oraz < i > i śmiga.
#include <string>
#include <iostream>
using namespace std;
string entitiesReplace( string str ) {
string subs[] = { ";", "&", """, "'", "<", ">", ":", "=", "!" };
string reps[] = { "#semi#", "#amp#", "#quot#", "#apos#", "#lt#", "#gt#", "#colon#", "#equals#", "#excl#" };
size_t found;
for( int j = 0; j < 9; j++ ) {
do {
found = str.find( subs[ j ] );
if( found != string::npos ) {
str.replace( found, subs[ j ].length(), reps[ j ] );
}
} while( found != string::npos );
}
return str;
}
string entitiesReplaceBack( string str ) {
string subs[] = { "#semi#", "#amp#", "#quot#", "#apos#", "#lt#", "#gt#", "#colon#", "#equals#", "#excl#" };
string reps[] = { ";", "&", """, "'", "<", ">", ":", "=", "!" };
size_t found;
for( int j = 0; j < 9; j++ ) {
do {
found = str.find( subs[ j ] );
if( found != string::npos ) {
str.replace( found, subs[ j ].length(), reps[ j ] );
}
} while( found != string::npos );
}
return str;
}
string entitiesDecode( string str ) {
string subs[] = { ";", "&", """, "'", "<", ">", ":", "=", "!" };
string reps[] = { ";", "&", "\"", "'", "<", ">", ":", "=", "!" };
size_t found;
for( int j = 0; j < 9; j++ ) {
do {
found = str.find( subs[ j ] );
if( found != string::npos ) {
str.replace( found, subs[ j ].length(), reps[ j ] );
}
} while( found != string::npos );
}
str = entitiesReplaceBack( str );
return str;
}
string entitiesEncode( string str ) {
str = entitiesReplace( str );
string subs[] = { ";", "&", "\"", "'", "<", ">", ":", "=", "!" };
string reps[] = { ";", "&", """, "'", "<", ">", ":", "=", "!" };
size_t found;
for( int j = 0; j < 9; j++ ) {
do {
found = str.find( subs[ j ] );
if( found != string::npos ) {
str.replace( found, subs[ j ].length(), reps[ j ] );
}
} while( found != string::npos );
}
return str;
}
int main() {
std::string html = "<h1>Hello " girls :) 'WooooW'! No i teraz jakiś \"CUDZYSLOWIE\". Nowe & zdanie.";
string encode = entitiesEncode( html );
cout << "Encode: " << encode << endl << endl;
string decode = entitiesDecode( encode );
cout << "Decode : " << decode << endl << endl;
}