Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

C++ html entities encode/decode

Ostatnio zmodyfikowano 2017-09-15 18:51
Autor Wiadomość
Breakermind
Temat założony przez niniejszego użytkownika
C++ html entities encode/decode
» 2017-09-14 15:25:58
Witam,
gdzie znajdę jakiś przykład lub gotowe funkcję równoważne z tymi z php:
$a = htmlentities($b, ENT_QUOTES, "UTF-8");
$b = html_entity_decode($a);

P-164874
darko202
» 2017-09-15 13:42:19
P-164916
Breakermind
Temat założony przez niniejszego użytkownika
» 2017-09-15 18:51:18
Dzięki to też znalazłem :) i takie coś:

http://www.ionflux.org/v2.0/en/code-snippets-utf-8.html
i
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.

C/C++
#include <string>
#include <iostream>

using namespace std;

string entitiesReplace( string str ) {
    string subs[] = { "&semi;", "&amp;", "&quot;", "&apos;", "&lt;", "&gt;", "&colon;", "&equals;", "&excl;" };
    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[] = { "&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 entitiesDecode( 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 );
       
    }
    str = entitiesReplaceBack( str );
    return str;
}

string entitiesEncode( string str ) {
    str = entitiesReplace( 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;
}

int main() {
   
    std::string html = "<h1>Hello &quot; girls :) 'WooooW'! No i teraz jakiś \"CUDZYSLOWIE\". Nowe &amp; zdanie.";
   
    // Encode entities for mysql
    string encode = entitiesEncode( html );
    cout << "Encode: " << encode << endl << endl;
   
    // Decode
    string decode = entitiesDecode( encode );
    cout << "Decode : " << decode << endl << endl;
   
}
P-164923
« 1 »
  Strona 1 z 1