adandelion

诗词在线 http:/www.chinapoesy.com


诗词在线 |唐诗|宋词|元曲|诗经|离骚|乐府|现代诗歌

博客园 首页 联系 订阅 管理
在HTML编辑器中编辑的内容在存入数据库前要替换成如下的格式:  
  <content>  
        <item   type=”text”>   hello   World!   </item>  
        <item   type=”textfile”>   http://www.humyu.com/App01/text01.txt   </item>  
        <item   type=”textfile”>   /App01/text01.txt</item>  
        <item   type=”image”>   http://www.humyu.com/app01/image/image01.jpg   </item>  
        <item   type=”image”>   /app01/image/image02.gif   </item>  
        <item   type=”voice”>   /app01/voice/voice01.amr   </item>  
        <item   type=”voice”>   http://www.humyu.com/app01/voice/voice02.amr   </item>  
  </content>  
  详细说明:  
   
  1.例子  
  HTML编辑器中内容如下:  
   
  你好、你好吗?<IMG   src="http://localhost/aaa/20058169483.gif">我好我好<IMG   src="http://localhost/aaa/200581694817.gif"><IMG    
   
  src="http://localhost/aaa/200581694822.gif">他好他好<TXT   src="http://www.humyu.com/App01/text01.txt">都好。都好  
   
  替换后个内容为:  
  <item   type="text">   你好、你好吗?</item>  
  <item   type="image">http://localhost/aaa/20058169483.gif   </item>  
  <item   type="text">   我好我好</item>  
  <item   type="image">http://localhost/aaa/200581694817.gif   </item>  
  <item   type="image">http://localhost/aaa/200581694822.gif   </item>  
  <item   type="text">   他好他好</item>  
  <item   type=”textfile”>http://www.humyu.com/App01/text01.txt</item>  
  <item   type="text">   都好。都好</item>  
   
  2.<item   type=”voice”>   /app01/voice/voice01.amr   </item>好说的,它是放末尾的一个文件而已,内容中有<=1个.这里我可以自己解决的.  
   
  3.<item   type="text">、<item   type="image">、<item   type=”textfile”>这三个的数量和位置是不固定的。拿<item   type="text">来说,它  
   
  在内容里可能在第一个就是它,也可能在后面,也可能没有它,也可能连续几个都是它。  
   
  3.其实就是把内容转换成xml格式。  
   
  我说清楚了吧,如果没清楚,请指出来,我立刻回复;先谢谢各位大虾了,谢谢!!

1 楼adandelion(水源是CSDN最黑的地方,但这个最黑是CSDN一手制造的!)回复于 2005-08-16 10:12:36 得分 0

备注:  
  1.其它的HTML标记不用考虑了.我里面只有图片,文字,<TXT   src="http://www.humyu.com/App01/text01.txt">.  
  2.<item   type=”voice”>   /app01/voice/voice01.amr   </item>这个我加在后面就可以了.Top

2 楼yingfeiqiyue(契约)回复于 2005-08-16 13:52:31 得分 1

正则表达式的问?  
  你可不可以判断<“IMG、>”等进行转换!  
  比如:你好、你好吗?<IMG   src="http://localhost/aaa/20058169483.gif">  
  找到<IMG   就将?“<IMG   src="”换为“<item   type="image">”  
  也可以用一个一个的转换,输入“你好、你好吗?”转为“<item   type="text">   你好、你好吗?</item>”输入“<IMG   src="http://localhost/aaa/20058169483.gif">”转为  
  “<item   type="image">http://localhost/aaa/20058169483.gif   </item>”  
  Top

3 楼fancyf(凡瑞)回复于 2005-08-16 14:53:37 得分 148

现在正则表达式的复杂度已经向系统化发展了,真头疼~  
   
  string   content   =   @"你好、你好吗?<IMG   src=""http://localhost/aaa/20058169483.gif"">我好我好<IMG   src=""http://localhost/aaa/200581694817.gif""><IMG    
   
  src=""http://localhost/aaa/200581694822.gif"">他好他好<TXT   src=""http://www.humyu.com/App01/text01.txt"">都好。都好";  
  //content   =   aRegex.Replace(content,   "");  
  Regex   htmlRegex   =   new   Regex(@"(^(?<text>[^<]+?)(?<e><))|((?<s>>)(?<text>[^<]+?)(?<e><))|((?<s>>)(?<text>[\S]+?)$)",   RegexOptions.IgnoreCase   |   RegexOptions.Compiled);  
  content   =   htmlRegex.Replace(content,   @"${s}<item   type=""text"">${text}</item>${e}");  
  htmlRegex   =   new   Regex(@"<img\s*?src=""(?<img>[^""]*?)"">",   RegexOptions.IgnoreCase   |   RegexOptions.Compiled);  
  content   =   htmlRegex.Replace(content,   @"<item   type=""image"">${img}</item>");  
  htmlRegex   =   new   Regex(@"<txt\s*?src=""(?<txt>[^""]*?)"">",   RegexOptions.IgnoreCase   |   RegexOptions.Compiled);  
  content   =   htmlRegex.Replace(content,   @"<item   type=""textfile"">${txt}</item>");  
   
  content   =   content.Replace("</item>",   "</item>\r\n");  
  Console.WriteLine(content);  
   
  输出:  
  <item   type="text">你好、你好吗?</item>  
  <item   type="image">http://localhost/aaa/20058169483.gif</item>  
  <item   type="text">我好我好</item>  
  <item   type="image">http://localhost/aaa/200581694817.gif</item>  
  <item   type="image">http://localhost/aaa/200581694822.gif</item>  
  <item   type="text">他好他好</item>  
  <item   type="textfile">http://www.humyu.com/App01/text01.txt</item>  
  <item   type="text">都好。都好</item>  
  好像挺逼真的Top

4 楼powerllr(笨笨的招财鸡)回复于 2005-08-16 15:07:44 得分 0

呀~楼上强Top

5 楼adandelion(水源是CSDN最黑的地方,但这个最黑是CSDN一手制造的!)回复于 2005-08-16 15:09:48 得分 0

非常感谢谢谢大虾!!!  
  偶看看Top

6 楼okyzx(Jason)回复于 2005-08-16 15:21:16 得分 0

:)Top

7 楼lmlive(逝去D甲虫)回复于 2005-08-16 15:36:03 得分 1

fancyf(凡瑞)   挺牛B的  
  其实可以在客户端实现这个处理过程  
  <script>  
  //调HTML编辑器名为Editor  
  function   getXml(){  
      var   img=Editor.document.images;  
      for(o   in   img){  
          str+="<item   type=image>"+img[o].src+"</item";  
      }  
      var   textstr=Editor.document.innerText;  
      for(o   in   textstr.split("\n"){  
          str+=textstr.split("\n")[o];  
      }  
      retrun   str;  
  }  
  </script>Top

8 楼adandelion(水源是CSDN最黑的地方,但这个最黑是CSDN一手制造的!)回复于 2005-08-16 16:05:41 得分 0

查看修改中....  
  谢谢帮助\关注.....Top

9 楼adandelion(水源是CSDN最黑的地方,但这个最黑是CSDN一手制造的!)回复于 2005-08-17 14:21:58 得分 0

fancyf(凡瑞)   你好!  
  只有文本如下的时候并没有生成<item   type="text">你好你好你好</item>  
  请再赐教,非常谢谢!  
   
  strContent="你好你好你好";  
  string   content   =   @strContent;  
  Regex   htmlRegex   =   new   Regex(@"(^(?<text>[^<]+?)(?<e><))|((?<s>>)(?<text>[^<]+?)(?<e><))|((?<s>>)(?<text>[\S]+?)$)",   RegexOptions.IgnoreCase   |   RegexOptions.Compiled);  
  content   =   htmlRegex.Replace(content,   @"${s}<item   type=""text"">${text}</item>${e}");  
  htmlRegex   =   new   Regex(@"<img\s*?src=""(?<img>[^""]*?)"">",   RegexOptions.IgnoreCase   |   RegexOptions.Compiled);  
  content   =   htmlRegex.Replace(content,   @"<item   type=""image"">${img}</item>");  
  //上传的txt文件/txt文件链接,都以超连接的形式显示  
  htmlRegex   =   new   Regex(@"<a[^>]*href=(""(?<href>[^""]*)""|'(?<href>[^']*)'|(?<href>[^\s>]*))[^>]*>(?<text>.*?)</a>",   RegexOptions.IgnoreCase   |   RegexOptions.Singleline);  
  content   =   htmlRegex.Replace(content,@"<item   type=""textfile"">${href}</item>");  
  content   =   content.Replace("</item>",   "</item>\r\n");  
  content   =   Regex.Replace(content,"<\\/??p>","\r\n",RegexOptions.IgnoreCase);  
  Response.Write(content);Top

10 楼adandelion(水源是CSDN最黑的地方,但这个最黑是CSDN一手制造的!)回复于 2005-08-17 14:37:40 得分 0

还有一点说明,我把原来的<TXT   src="http://www.humyu.com/App01/text01.txt">替换成了<a   href="http://www.humyu.com/App01/text01.txt">http://www.humyu.com/App01/text01.txt</a>  
  这里应该没问题的.Top

11 楼lxinxuan(林子)回复于 2005-08-17 14:37:41 得分 0

有趣  
  关注中  
  上次随便写一个,将html代码胡乱编辑...真好玩一个Top

12 楼fancyf(凡瑞)回复于 2005-08-17 15:42:32 得分 0

疏忽了~把第一组替换改了一下,再试试吧  
  原来的:  
  Regex   htmlRegex   =   new   Regex(@"(^(?<text>[^<]+?)(?<e><))|((?<s>>)(?<text>[^<]+?)(?<e><))|((?<s>>)(?<text>[\S]+?)$)",   RegexOptions.IgnoreCase   |   RegexOptions.Compiled);  
  content   =   htmlRegex.Replace(content,   @"${s}<item   type=""text"">${text}</item>${e}");  
   
  改成:  
  Regex   htmlRegex   =   new   Regex(@"(?<=^|>)(?<text>[^<]+?)(?=<|$)",   RegexOptions.IgnoreCase   |   RegexOptions.Compiled);  
  content   =   htmlRegex.Replace(content,   @"<item   type=""text"">${text}</item>");Top

13 楼adandelion(水源是CSDN最黑的地方,但这个最黑是CSDN一手制造的!)回复于 2005-08-17 15:51:35 得分 0

非常谢谢!!  
  我再试试!!Top

14 楼adandelion(水源是CSDN最黑的地方,但这个最黑是CSDN一手制造的!)回复于 2005-08-17 17:27:34 得分 0

谢谢了,OK了  
    fancyf(凡瑞)  
  佩服地我是五体投地!!!

posted on 2007-07-10 12:22  猪头  阅读(400)  评论(0)    收藏  举报

欢迎访问诗词在线http://www.chinapoesy.com   诗词在线 |唐诗|宋词|元曲|诗经|离骚|乐府|古典诗歌|现代诗歌|古典诗词|现代诗词|诗歌可以陶冶你的情操、丰富你的生活,让你更具内涵。诗词在线打造中国最好的诗词社区!

诗词在线社区

126在线阅读网 历史书籍、文学书籍、小说。。。