【问题标题】:How to decode HTML Entities in C?如何在 C 中解码 HTML 实体?
【发布时间】:2009-07-04 12:54:20
【问题描述】:

我对取消转义文本感兴趣,例如:\ 映射到 C 中的 \。有人知道一个好的库吗?

参考维基百科List of XML and HTML Character Entity References

【问题讨论】:

    标签: html c


    【解决方案1】:

    有关 C 中解码这些 HTML 实体的另一个开源参考,您可以查看命令行实用程序 uni2ascii/ascii2uni。相关文件是用于实体查找的 enttbl.{c,h} 和从 UTF32 向下转换为 UTF8 的 putu8.c。

    uni2ascii

    【讨论】:

    • 正是我想要的。
    【解决方案2】:

    我写了自己的 unescape 代码;非常简化,但确实有效:pn_util.c

    【讨论】:

      【解决方案3】:

      功能说明:将特殊的 HTML 实体转换回字符。 需要进行一些修改以满足您的要求。

      char* HtmlSpecialChars_Decode(char* encodedHtmlSpecialEntities)
      {
      int encodedLen = 0;
      int escapeArrayLen = 0;
      static char decodedHtmlSpecialChars[TITLE_SIZE];
      char innerHtmlSpecialEntities[MAX_CONFIG_ITEM_SIZE];
      
      /* This mapping table can be extended if necessary. */
      static const struct {
          const char* encodedEntity;
          const char decodedChar;
      } entityToChars[] = {
              {"&lt;", '<'},
              {"&gt;", '>'},
              {"&amp;", '&'},
              {"&quot;", '"'},
              {"&#039;", '\''},
          };
      
      if(strchr(encodedHtmlSpecialEntities, '&') == NULL)
          return encodedHtmlSpecialEntities;
      
      memset(decodedHtmlSpecialChars, '\0', TITLE_SIZE);
      memset(innerHtmlSpecialEntities, '\0', MAX_CONFIG_ITEM_SIZE);
      escapeArrayLen = sizeof(entityToChars) / sizeof(entityToChars[0]);
      
      
      strcpy(innerHtmlSpecialEntities, encodedHtmlSpecialEntities);
      encodedLen = strlen(innerHtmlSpecialEntities);
      
      for(int i = 0; i < encodedLen; i++)
      {
          if(innerHtmlSpecialEntities[i] == '&')
          {
              /* Potential encode char. */
              char * tempEntities = innerHtmlSpecialEntities + i;
      
              for(int j = 0; j < escapeArrayLen; j++)
              {
                  if(strncmp(tempEntities, entityToChars[j].encodedEntity, strlen(entityToChars[j].encodedEntity)) == 0)
                  {
                      int index = 0;
                      strncat(decodedHtmlSpecialChars, innerHtmlSpecialEntities, i);
      
                      index = strlen(decodedHtmlSpecialChars);
                      decodedHtmlSpecialChars[index] = entityToChars[j].decodedChar;
                      if(strlen(tempEntities) > strlen(entityToChars[j].encodedEntity))
                      {
                          /* Not to the end, continue */
                          char temp[MAX_CONFIG_ITEM_SIZE] = {'\0'};
                          strcpy(temp, tempEntities + strlen(entityToChars[j].encodedEntity));
                          memset(innerHtmlSpecialEntities, '\0', MAX_CONFIG_ITEM_SIZE);
                          strcpy(innerHtmlSpecialEntities, temp);
      
                          encodedLen = strlen(innerHtmlSpecialEntities);
                          i = -1;
                      }
                      else
                          encodedLen = 0;
      
                      break;
                  }
              }
          }
      }
      
      if(encodedLen != 0)
          strcat(decodedHtmlSpecialChars, innerHtmlSpecialEntities);
      
      return decodedHtmlSpecialChars;
      

      }

      【讨论】:

        【解决方案4】:
        QString UNESC(const QString &txt) {
            QStringList bld;
            static QChar AMP = '&', SCL = ';';
            static QMap<QString, QString> dec = {
                {"&lt;", "<"}, {"&gt;", ">"}
              , {"&amp;", "&"}, {"&quot;", R"(")"}, {"&#039;", "'"} };
        
            if(!txt.contains(AMP)) { return txt; }
        
            int bgn = 0, pos = 0;
            while((pos = txt.indexOf(AMP, pos)) != -1) {
                int end = txt.indexOf(SCL, pos)+1;
                QString val = dec[txt.mid(pos, end - pos)];
        
                bld << txt.mid(bgn, pos - bgn);
        
                if(val.isEmpty()) {
                    end = txt.indexOf(AMP, pos+1);
                    bld << txt.mid(pos, end - pos);
                } else {
                    bld << val;
                }// else // if(val.isEmpty())
        
                bgn = end; pos = end;
            }// while((pos = txt.indexOf(AMP, pos)) != -1)
        
            return bld.join(QString());
        }// UNESC
        

        【讨论】:

          猜你喜欢
          • 2014-10-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多