How to encode text into html format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred Yu

    #1

    How to encode text into html format

    Hi,

    I want to encode input text into html format such as replace "<" with "&lt",
    replace "&" with "&amp".
    Could you give me some ideas? Thanks.

    Fred




  • Sam

    #2
    Re: How to encode text into html format

    Fred Yu writes:
    Hi,
    >
    I want to encode input text into html format such as replace "<" with "&lt",
    That's "&lt;".
    replace "&" with "&amp".
    That's "&amp;".
    Could you give me some ideas? Thanks.
    Try to do your homework assignment by yourself. This is a simple
    search/replace operation, and there are many ways to get it done.


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEABECAAYFAkh C4aQACgkQx9p3GY HlUOJbHQCdFkadJ pXTIStsphfd9cZ1 yjhc
    4QwAniVg+D//DKToDNz651FJF5M CYIlo
    =xhO4
    -----END PGP SIGNATURE-----

    Comment

    • Kai-Uwe Bux

      #3
      Re: How to encode text into html format

      Fred Yu wrote:
      Hi,
      >
      I want to encode input text into html format such as replace "<" with
      "&lt", replace "&" with "&amp".
      Could you give me some ideas? Thanks.

      Containers: std::map< char, std::string >
      Iterators: std::istream_it erator, std::ostream_it erator
      Algorithms: std::transform


      Best

      Kai-Uwe Bux

      Comment

      • AnonMail2005@gmail.com

        #4
        Re: How to encode text into html format

        On Jun 1, 12:37 pm, "Fred Yu" <jean_y_f...@so hu.comwrote:
        Hi,
        >
        I want to encode input text into html format such as replace "<" with "&lt",
        replace "&" with "&amp".
        Could you give me some ideas? Thanks.
        >
        Fred
        google iconv. It will convert from many char encodings to many other
        char
        encodings. I've used it to "format" text in various XML wrapper
        classes.

        Comment

        • Elmar Baumann

          #5
          Re: How to encode text into html format


          "Fred Yu" <jean_y_f398@so hu.comschrieb im Newsbeitrag
          news:g1uka7$o1g $2@news.cn99.co m...
          Hi,
          >
          I want to encode input text into html format such as replace "<" with
          "&lt",
          replace "&" with "&amp".
          Example for AnsiString Class

          AnsiString Input; //contains the html code
          int pos;

          do // replace "<" to "&lt"
          {
          if(Input.Pos("< ") NULL)
          {
          pos = Input.Pos("<");
          Input.Delete(po s,1);
          Input.Insert("% 26lt",pos);
          }
          }
          while(Input.Pos ("<") NULL);



          Comment

          • James Kanze

            #6
            Re: How to encode text into html format

            On Jun 1, 8:11 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
            Fred Yu wrote:
            I want to encode input text into html format such as replace "<" with
            "&lt", replace "&" with "&amp".
            Could you give me some ideas? Thanks.
            Containers: std::map< char, std::string >
            Iterators: std::istream_it erator, std::ostream_it erator
            Algorithms: std::transform
            Agreed for the first (although it may be overkill---in this
            particular case, I think I'd go with a simple switch).

            No real need for the second; just use istream::get() and
            ostream::put() (or operator<< in some cases).

            As to the third: how? You're replacing a single character with
            a sequence of characters, and transform does a one to one (which
            in practice makes it of fairly limited utility---although I've
            used it with a vector<string>, ostream_iterato r, and as string
            transformer class that I've written, which works something like
            $(patsubst...) in GNU make).

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • Kai-Uwe Bux

              #7
              Re: How to encode text into html format

              James Kanze wrote:
              On Jun 1, 8:11 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
              >Fred Yu wrote:
              I want to encode input text into html format such as replace "<" with
              "&lt", replace "&" with "&amp".
              Could you give me some ideas? Thanks.
              >
              >Containers: std::map< char, std::string >
              >Iterators: std::istream_it erator, std::ostream_it erator
              >Algorithms: std::transform
              >
              Agreed for the first (although it may be overkill---in this
              particular case, I think I'd go with a simple switch).
              >
              No real need for the second; just use istream::get() and
              ostream::put() (or operator<< in some cases).
              >
              As to the third: how? You're replacing a single character with
              a sequence of characters, and transform does a one to one (which
              in practice makes it of fairly limited utility---although I've
              used it with a vector<string>, ostream_iterato r, and as string
              transformer class that I've written, which works something like
              $(patsubst...) in GNU make).
              I was thinking of something like this:

              #include <iostream>
              #include <iterator>
              #include <map>
              #include <algorithm>
              #include <cassert>

              struct encoder {

              std::map< char, std::string the_map;

              encoder ( void ) {
              the_map[ 'a' ] = "a";
              // ...
              the_map[ '&' ] = "&amp";
              // ...
              }

              std::string const & operator() ( char ch ) const {
              std::map< char, std::string >::const_iterat or iter =
              the_map.find( ch );
              assert( iter != the_map.end() );
              return ( iter->second );
              }
              };

              int main ( void ) {
              encoder the_encoder;
              std::transform( std::istreambuf _iterator<char> ( std::cin ),
              std::istreambuf _iterator<char> (),
              std::ostream_it erator<std::str ing>( std::cout, "" ),
              the_encoder );
              }


              Best

              Kai-Uwe Bux

              Comment

              • Frank Birbacher

                #8
                Re: How to encode text into html format

                Hi!

                James Kanze schrieb:
                As to the third: how? You're replacing a single character with
                a sequence of characters, and transform does a one to one (which
                in practice makes it of fairly limited utility---although I've
                used it with a vector<string>, ostream_iterato r, and as string
                transformer class that I've written, which works something like
                $(patsubst...) in GNU make).
                The source range of transform may have another value type than the
                destination range.

                char const* replace(char);

                transform(str.b egin(), str.end(),
                ostream_iterato r<const char*>(cout),
                &replace);

                Frank

                Comment

                • James Kanze

                  #9
                  Re: How to encode text into html format

                  On Jun 1, 11:01 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
                  James Kanze wrote:
                  On Jun 1, 8:11 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
                  Fred Yu wrote:
                  I want to encode input text into html format such as
                  replace "<" with "&lt", replace "&" with "&amp". Could
                  you give me some ideas? Thanks.
                  Containers: std::map< char, std::string >
                  Iterators: std::istream_it erator, std::ostream_it erator
                  Algorithms: std::transform
                  Agreed for the first (although it may be overkill---in this
                  particular case, I think I'd go with a simple switch).
                  No real need for the second; just use istream::get() and
                  ostream::put() (or operator<< in some cases).
                  As to the third: how? You're replacing a single character
                  with a sequence of characters, and transform does a one to
                  one (which in practice makes it of fairly limited
                  utility---although I've used it with a vector<string>,
                  ostream_iterato r, and as string transformer class that I've
                  written, which works something like $(patsubst...) in GNU
                  make).
                  I was thinking of something like this:
                  #include <iostream>
                  #include <iterator>
                  #include <map>
                  #include <algorithm>
                  #include <cassert>
                  struct encoder {
                  std::map< char, std::string the_map;
                  encoder ( void ) {
                  the_map[ 'a' ] = "a";
                  // ...
                  the_map[ '&' ] = "&amp";
                  // ...
                  }
                  std::string const & operator() ( char ch ) const {
                  std::map< char, std::string >::const_iterat or iter =
                  the_map.find( ch );
                  assert( iter != the_map.end() );
                  return ( iter->second );
                  }
                  };
                  int main ( void ) {
                  encoder the_encoder;
                  std::transform( std::istreambuf _iterator<char> ( std::cin ),
                  std::istreambuf _iterator<char> (),
                  std::ostream_it erator<std::str ing>( std::cout, "" ),
                  the_encoder );
                  }
                  Which looks like a lot of overhead (including in terms of
                  programming) for very little gain. It might be worth it if you
                  create some sort of generic encoder, in order to reuse the idiom
                  in many different contexts, but for such a simple problem, it
                  just seems overkill for a onetime solution. As I said, I'd
                  probably go with the switch. If I were going to go to the
                  effort of initializing the map completely, I'd probably go with
                  a char const*[UCHAR_MAX], rather than std::map. Or a map with
                  just the elements which don't use an identity transformation.
                  And I'd probably still write out the loop; somehow, the idea of
                  transforming each individual character into a string just to
                  output it bothers me.

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  • James Kanze

                    #10
                    Re: How to encode text into html format

                    On Jun 1, 11:25 pm, Frank Birbacher <bloodymir.c... @gmx.netwrote:
                    James Kanze schrieb:
                    As to the third: how? You're replacing a single character with
                    a sequence of characters, and transform does a one to one (which
                    in practice makes it of fairly limited utility---although I've
                    used it with a vector<string>, ostream_iterato r, and as string
                    transformer class that I've written, which works something like
                    $(patsubst...) in GNU make).
                    The source range of transform may have another value type than the
                    destination range.
                    I'm aware of that, however...
                    char const* replace(char);
                    transform(str.b egin(), str.end(),
                    ostream_iterato r<const char*>(cout),
                    &replace);
                    For some reason, I was thinking in terms of std::string, and not
                    char const*. And converting each std::string seemed a bit heavy
                    for the task at hand. But a statically generated char const*[];
                    why not?

                    --
                    James Kanze (GABI Software) email:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                    Comment

                    Working...