Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved html encoding

    General and Desktop
    5
    11
    1999
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mrdebug
      mrdebug last edited by

      Hi, is there a function to translate a standard charset word to html? For example:

      "perché" -> "perché"
      

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      raven-worx 1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        https://doc.qt.io/qt-5/qstring.html#toHtmlEscaped

        (Z(:^

        1 Reply Last reply Reply Quote 2
        • sierdzio
          sierdzio Moderators last edited by

          Hm although that probably won't work for é character :/

          (Z(:^

          1 Reply Last reply Reply Quote 0
          • sierdzio
            sierdzio Moderators last edited by

            Perhaps QUrl would work better here https://doc.qt.io/qt-5/qurl.html#toPercentEncoding

            (Z(:^

            1 Reply Last reply Reply Quote 0
            • raven-worx
              raven-worx Moderators @mrdebug last edited by

              @mrdebug
              you can traverse the string char-wise and replace each unicode character >= 128 with &#<num> or &#x<unicode-hex-val>
              For the named entities (like &eacute;) you once will need to create a static replacement map.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply Reply Quote 6
              • JonB
                JonB last edited by

                @sierdzio

                Perhaps QUrl would work better here https://doc.qt.io/qt-5/qurl.html#toPercentEncoding

                I have been known in the past to "cheat" by using url encoding rules on HTML (e.g. in JavaScript) like @sierdzio suggests. But be aware the rules are not the same (HTML encoing != URL encoding), it's naughty and does not work in all cases.

                Like @raven-worx says, if you want things like &eacute; you have to do this by hand every time, following voluminous lists like the reference he gives. Why languages/libraries do not seem to supply a useful function for this because it gets asked/wanted a lot, I do not know.

                1 Reply Last reply Reply Quote 3
                • mrdebug
                  mrdebug last edited by

                  Many thanks for your support. I need only a function to translate the IoT device descriptions in a correct html format to send notification emails.
                  So this function is enough for me.

                  QString TextToHTML(QString Value) {
                      Value.replace("à", "&agrave;");
                      Value.replace("è", "&egrave;");
                      Value.replace("é", "&eacute;");
                      Value.replace("ì", "&igrave;");
                      Value.replace("ò", "&ograve;");
                      Value.replace("ù", "&agrave;");
                      return Value;
                  }
                  

                  This function is not so cool but could be enough.

                  Need programmers to hire?
                  www.labcsp.com
                  www.denisgottardello.it
                  GMT+1
                  Skype: mrdebug

                  JonB raven-worx 2 Replies Last reply Reply Quote 0
                  • JonB
                    JonB @mrdebug last edited by JonB

                    @mrdebug
                    Yes this is what you have to do. However, while you are there, what are you doing about any characters which must be entitized for correct HTML, like < -> &lt; ? I always do at least the 4: &lt;, &amp;, &quot; & &apos;. @sierdzio 's https://doc.qt.io/qt-5/qstring.html#toHtmlEscaped will do you for that. You might like to call that [at the start of, not the end!] in your TextToHTML() function.

                    aha_1980 1 Reply Last reply Reply Quote 0
                    • raven-worx
                      raven-worx Moderators @mrdebug last edited by raven-worx

                      @mrdebug said in html encoding:
                      instead of

                      Value.replace("à", "&agrave;");
                      

                      better do

                      Value.replace( QChar(0x00C0)/*à*/,  QStringLiteral("&agrave;"));
                      

                      it's more robust.

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply Reply Quote 0
                      • aha_1980
                        aha_1980 Lifetime Qt Champion @JonB last edited by

                        @JonB said in html encoding:

                        @mrdebug
                        Yes this is what you have to do. However, while you are there, what are you doing about any characters which must be entitized for correct HTML, like < -> &lt; ? I always do at least the 4: &lt;, &amp;, &quot; & &apos;.

                        That is, as @sierdzio already pointed out, https://doc.qt.io/qt-5/qstring.html#toHtmlEscaped

                        Qt has to stay free or it will die.

                        JonB 1 Reply Last reply Reply Quote 1
                        • JonB
                          JonB @aha_1980 last edited by

                          @aha_1980 Yep, missed that, corrected my post.

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post