Hi all..
I'm making a custom HTMLarea for myself, i'm using special tags i made to modify text formating, like the size and color,......., Just like this forums HTMLarea
then i replace this tags with html tags "<font size=1>some text here</font>" using the Replace method for string, and it works pretty fine, but the problem is that it changes some text that i dont want change, like if i posted a code containge "[]" for looping for example.
So what i want is to loop through the text variable and change each tag i want to change. How can i do that?!
I had written this block of code a few weeks ago to strip out any html tags in the text. _body is the string variable I used to hold the text loaded from a database.
Code:
Dim _body as string = "<b>Test</b>. <u>This is </u> a test to remove formatting."
Dim _lt As String = "<"
Dim _gt As String = ">"
Dim _value As Integer = _body.IndexOf(_lt)
Dim _value2 As Integer = _body.IndexOf(_gt, _value) + 1
_body = _body.Replace(_body.Substring(_value, _value2 - _value), "")
You could modify this to replace the text between the tags instead of removing the tags like I did.
The load method allow you the load a entire doc into class where the LoadXml method allows you to load just a xml string (you probably more interested in this method...)
for example, if you have the following custom html:
dim htmlString as String = "<font size='1'>some text here</font>"
you can strip it with the following snippet of code:
Code:
'method stripe html tag and put inner string
Public Function stripHTMLString(ByVal htmlString As String) As String
Dim xmlString As System.Xml.XmlDocument = New System.Xml.XmlDocument
xmlString.LoadXml(htmlString)
Return xmlString.InnerText
End Function
to use the function (supposed we created an object call HTMLDriver):
Response.write(HTMLDriver.stripHTMLString(htmlString))
for complex custom html tag, u can even use xpath to further customer and control the inner text as well.
see if that helps...
"Java is great, PHP sucks and .NET can't scale" - Shaun Connoly, JBoss's vice president of product development. April 5, 2006
i'll show u example of what i tried to do to understand more what i want, but i think now we are getting so close
Code:
private string ModifyBody(string Body)
{
//Modify The Size
char[] charBody = Body.ToCharArray();
StringBuilder sb = new StringBuilder();
string strModBody = "";
string strBody = "";
for (int i = 0; i < charBody.Length; i++)
{
sb.Append(charBody[i].ToString());
//strBody = sb.ToString();
if (sb.ToString().IndexOf("[size=") != -1 && sb.ToString().IndexOf("[/size]") != -1)
{
sb.Replace("]", ">");
sb.Replace("[size=", "<font size=");
sb.Replace("[/size", "</font");
}
}
Body = sb.ToString();//strModBody;
return Body;
}
now this works pretty fine, but the problem is in this tags
Code:
"]", ">"
that if there's other tags between the size tag it will change which i dont want to happen.
so, i tried to use this but i dont know how to make it work
Code:
for (int i = 0; i < charBody.Length; i++)
{
sb.Append(charBody[i].ToString());
strBody = sb.ToString();
if (sb.ToString().IndexOf("[size=") != -1 && sb.ToString().IndexOf("[/size]") != -1)
{
//i think this doesnt work fine in the loop
strModBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
strModBody = strModBody.Replace("[size=", "<font size=");
strModBody = strModBody.Replace("[/size]", "</font>");
strModBody = strModBody.Replace("]", ">");
}
}
I think Regular expressions is so boring, it contains alot of sybmols that will take alot of time to understand, and i think it will work better for validating data than replacing text if string variable. dnt you think?!
isnt there better way to do so with normal substring and replacr methods!
I think Regular expressions is so boring, it contains alot of sybmols that will take alot of time to understand, and i think it will work better for validating data than replacing text if string variable. dnt you think?!
isnt there better way to do so with normal substring and replacr methods!
I'm sure there are always alternative. However, I won't be the guy who can judge what is "better" and what is not, I tend to use what I feel is right.
I think the reason I pick regular expression as a solution implementation is because I personally want to match just beyong the char (]) problem. I was more concern of invalid tags (i.e: [font size='1'] [font size='3'] [/font]). Using regular expression will allows me to watch out of invalid tag first before i ever try to phase them into html.
I think you can combine methods from string class to accomplish what you wanted, if you use the "look ahead" approach. Meaning you have to prase the entire string into memory first, and then make mutiple trips (read entire string backward and forward) to make sure the logic (tag match, tag attribute match, etc) is in place.
Good luck with you program.
"Java is great, PHP sucks and .NET can't scale" - Shaun Connoly, JBoss's vice president of product development. April 5, 2006
Well, thank you, i've been reading in regex for long and i think it will take me long time to do what i want, would you check this out?!
1st, here's the original text
Code:
[size= 7]just new function[/size]
[size= 5]all new body function[/size]
[color= #A0522D]Color Text[/color]
[size= 3]the one body function[/size]
and here's what i'm trying to do
Code:
private string ModifyBody(string Body)
{
//Modify The Size
char[] charBody = Body.ToCharArray();
StringBuilder sb = new StringBuilder();
string strBody = "";
string modBody = "";
for (int i = 0; i < charBody.Length; i++)
{
sb.Append(charBody[i].ToString());
strBody = sb.ToString();
if (strBody.IndexOf("[size=") != -1 && strBody.IndexOf("[/size]") != -1)
{
// cut the tags and text in tags and put them in other string
modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
// modify the tags and replace them with html tags
modBody = modBody.Replace("[size=", "<font size=");
modBody = modBody.Replace("[/size", "</font");
modBody = modBody.Replace("]", ">");
// place the new modified string in the sbvariable
sb = sb.Insert(strBody.IndexOf("[size="), modBody);
//sb = sb.Remove(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
strBody = sb.ToString();
break;
//aModBody += strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7)) + "<br>";
sb.Remove(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
//strBody = sb.ToString();
//break;
}
}
Body = strBody;
return Body;
}
the problem is in the loop, when i dont use the break; statement i get this error:
Index and length must refer to a location within the string.
Parameter name: length
Exception Details: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
Line 67: modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
I dont know why it doesnt continue to the rest of the string!!!!!!!!!!!!!!!!!
for (int i = 0; i < charBody.Length; i++)
{
sb.Append(charBody[i].ToString());
strBody = sb.ToString();
if (strBody.IndexOf("[size=") != -1 && strBody.IndexOf("[/size]") != -1)
{
// cut the tags and text in tags and put them in other string
modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
// modify the tags and replace them with html tags
modBody = modBody.Replace("[size=", "<font size=");
modBody = modBody.Replace("[/size", "</font");
modBody = modBody.Replace("]", ">");
strBody = strBody.Replace(strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7)), modBody);
}//end if
}//end for
"Java is great, PHP sucks and .NET can't scale" - Shaun Connoly, JBoss's vice president of product development. April 5, 2006
Well, it works fine, but i still get the same problem within the loop which in the same line:
modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
check out this image i took while trying to debug it, the strBody seems correct, i dont know why the substring method dont work in the loop!!!!!!!!!!!!!
looks like the problem is at:
modBody = strBody.Substring(strBody.IndexOf("[size="), (strBody.IndexOf("[/size]") + 7));
when your input string has additional 7 or more spaces after [/size], then you are fine. One of your ending string was [/size], therefore + 7 will cause error:
Index and length must refer to a location within the string.
"Java is great, PHP sucks and .NET can't scale" - Shaun Connoly, JBoss's vice president of product development. April 5, 2006
hey, i removed the (+7) and still the error remains, i uses so that i can select the end of "[/size ]" tag, other wise it wont select it.
I'm using variable instead now but ofcourse the same problem
Code:
string rtg = "[size=";
string ltg = "[/size]";
if (strBody.IndexOf(rtg) != -1 && strBody.IndexOf(ltg) != -1)
{
// cut the tags and text in tags and put them in other string
modBody = strBody.Substring(strBody.IndexOf(rtg), (strBody.IndexOf(ltg) + ltg.Length));
Bookmarks