【Vegas原创】用正则表达式解决FCKEditor图片路径问题

2年前 (2022) 程序员胖胖胖虎阿
164 0 0

在用FCKEditor发邮件时,正文中图片会显示不出来,因为它默认路径是userfiles/images/*.jpg,如下

<input type="image" height="131" width="139" 

src="/userfiles/image/_W@S2WETFST%25F%25Z21AQCI3P.JPG" />

怎么转换为:


<input type="image" height="131" width="139" src="\\[Server]\userfiles\image\_W@S2WETFST%25F%25Z21AQCI3P.JPG" />

asp解法:

'邮件正文
    strsql="select txt,Filename,File_Name from bbs where unique_id="+Request.QueryString("Unique_ID")
    
set rs=connection.execute(strsql)
        
    myTxt 
= rs("txt")
    
'利用正则表达式替换img标记

dim objRe
set objRe = New RegExp
'設定正則式

objRe.Pattern 
= "(src=)('|"&CHR(34)&"| )?(.[^'| |"&CHR(34)&"]*)(\.)(jpg|gif|png|bmp|jpeg)('|"&CHR(34)&"| |>)?"

objRe.IgnoreCase = true
objRe.Multiline 
= true
objRe.Global 
= true
set matches = objRe.Execute(myTxt)
newtxt 
= myTxt
for each match in matches
    
    cccc 
= split(match.value,"/")
    
if(ubound(cccc) <> 0) then
    
'获得应该的字符串
    for i=0 to ubound(cccc)
        
if i = 0 then
            mystr 
= cccc(0)&"\\[server]\"
            
        
else if  i= ubound(cccc) then
            mystr 
= mystr&cccc(i) 
        
else
    
            mystr 
= mystr&cccc(i)&"\"
            
        end 
if
    end 
if
        
    next
    newtxt 
= Replace(newtxt,match.value,mystr)
    end 
if

    

next

set objRE = nothing

    a = "<body background='\\[server]\2008back.gif'>"
    
    Body
=a& newtxt &"</body>"
    
    

====================================================================

.Net 解法:


using System.Text.RegularExpressions;

 

 string   convertExp(string strTxt)
    {
       
        
string strPattern = "(src=)('|" + (char)34 + "| )?(.[^'| |" + (char)34 + "]*)(\\.)(jpg|gif|png|bmp|jpeg)('|" + (char)34 + "| |>)?";
        
// Compile the regular expression.
        Regex objRe=new Regex(strPattern);
        
// Match the regular expression pattern against a text string.
        MatchCollection matches=objRe.Matches(strTxt);
        
string mystr="";
        
string strNewTxt = strTxt;
      
        
foreach (Match match in matches)
        {
          
                
string[] strC = match.Value.Split('/');
                  
//if it's the bottom jpg,break Foreach
                if (strC[strC.Length - 1].ToString() == "asdf.jpg\"")
                {
                    
break;
                }
                
if (strC.Length != 0)
                {
                    
for (int i = 0; i < strC.Length; i++)
                    {
                        
if (i == 0)
                            mystr 
= strC[0+ "\\\\[server]\\";
                        
else if (i == strC.Length - 1)
                            mystr 
= mystr + strC[i];
                        
else
                            mystr 
= mystr + strC[i] + "\\";
                    }
                    strNewTxt 
= strNewTxt.Replace(match.Value, mystr);
                }
           
        }
        
return strNewTxt;

    }

调用:

 StringBuilder sb = getMailContent(strSubject);

        lblPreview.Text = convertExp(sb.ToString());

相关文章

暂无评论

暂无评论...