htmlencode和htmldecode,在线等待,比较急

chenccj 2008-12-04 10:14:29
在vc中如何实现象asp中的htmlencode和htmldecode,就是编码解码,请多帮忙。
...全文
168 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
CodeProject-Jerry 2008-12-11
  • 打赏
  • 举报
回复

#include <algorithm>
#include <iostream>
#include <string>

#define array_length(array) (sizeof (array) / sizeof (array)[0])

namespace Raye {
using namespace std;

struct HTMLReplace {
string match;
string replace;
} codes[] = {
{"&", "&"},
{"<", "<"},
{">", ">"}
};

string HTMLEncode( const string& s )
{
string rs = s;

// Replace each matching token in turn
for ( size_t i = 0; i < array_length( codes ); i++ ) {
// Find the first match
const string& match = codes[i].match;
const string& repl = codes[i].replace;
string::size_type start = rs.find_first_of( match );

// Replace all matches
while ( start != string::npos ) {
rs.replace( start, match.size(), repl );
// Be sure to jump forward by the replacement length
start = rs.find_first_of( match, start + repl.size() );
}
}

return rs;
}
}

int main()
{
using namespace std;

cout << Raye::HTMLEncode( "template <class T> void foo( const string& bar );" ) << '\n';

return 0;
}
littlepboy 2008-12-10
  • 打赏
  • 举报
回复
用CString或者string,比如CString:

void HtmlEncode(CString &sz)
{
sz.Replace("<", "<");
sz.Replace(">", ">");
...
}

效果:
输入:"The image tag: <img>",输出:"The image tag: <img>"
其他编码方式类似实现,解码也是类似实现

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧