日期:2014-05-18  浏览次数:20408 次

请问后台如何使用replace替换下列字符?
我有一个这样的字符
[localimg=400,300]1.jpg[/localimg]
请问在后台如何替换成
<img   src= "1.jpg "   width= "400 "   height= "300 "/>
注意!400,300是根据图片的宽度和高度变化的也就是这两个数值是变化的,1.jpg也是一样!

------解决方案--------------------
帮顶下````这个替换麻烦喽````
------解决方案--------------------
string b = "[localimg=400,300]1.jpg[/localimg] ";
b = System.Text.RegularExpressions.Regex.Replace(b, "\\[localimg=*([0-9]*),*([0-9]*)\\](.[^\\[]*)\\[\\/localimg] ", " <img src= '$3 ' width = $1 height=$2 > ", RegexOptions.IgnoreCase);
Response.Write(b);
------解决方案--------------------
string b = "[localimg=400,300]1.jpg[/localimg] ";
b = System.Text.RegularExpressions.Regex.Replace(b, "\\[localimg=(\\S+?),(\\S+?)\\](\\S+?)\\[\\/localimg] ", " <img src=\ "$3\ " width = \ "$1\ " height=\ "$2\ " /> ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Response.Write(b);

------解决方案--------------------
string str = @ "[localimg=400,300]1.jpg[/localimg] ";
Match m = Regex.Match(str, @ "^\[localimg=(\d{3}),(\d{3})\]\s*([^[].*)\s*\[/localimg\] ");
string s1=string.Empty;
string s2 = string.Empty;
string s3 = string.Empty;
if (m.Success)
{
s1 = m.Groups[1].Value.ToString();
s2 = m.Groups[2].Value.ToString();
s3 = m.Groups[3].Value.ToString();
}
string result=@ " <img src= ' "+s3+ " ' width= ' "+s1+ " ' height= ' "+s2+ " '/> ";
Response.Write(Server.HtmlEncode(result));
------解决方案--------------------
楼上正解~~~附上一个比较笨的原始办法:
private string run(string str)
{
string str1 = str.Replace( "[localimg= ", "[ ");
string str2 = str1.Replace( "[/localimg] ", " ");
//变为[400,300]1.jpg
//400,300
string str3 = str2.Split( '] ')[0].Replace( "[ ", " ");
//1.jpg
string str4 = str2.Split( '] ')[1];
//400
string str5 = str3.Split( ', ')[0];
//300
string str6 = str3.Split( ', ')[1];

return " <img src=\ " " + str4 + "\ " width=\ " " + str5 + "\ " height=\ " " + str6 + "\ "> ";
}

------解决方案--------------------
string oldstr= "[localimg=400,300]1.jpg[/localimg] ";
string imagename= "1.jpg ";
int imagewidth=300;
int imageheight=400;
string replacestr= " <img src= "+imagename+ " width= "+imagewidth+ " height= "+imageheight+ "/> "
string newstr=oldstr.Replace( "[localimg=400,300]1.jpg[/localimg] ",replacestr);


------解决方案--------------------
先读取需要的数据,然后再构造你所需要的
------解决方案--------------------
。。。顶LZ的行为~
------解决方案--------------------
时间太匆忙,这样看上去有点乱.但可以达到你想要的效果