PDA

View Full Version : HtmlTrimmer !



amir_3d_mad
یک شنبه 05 مهر 1388, 12:32 عصر
سلام .

من میخوام رشته ای رو با طول دلخواه نمایش بدم .

یعنی برای مثال فقط 300 کاراکتر اولش نمایش داده بشه !

اینکار رو با استفاده از تابع left انجام دادم . ولی اینکار باعث شد که کدهای html داخل رشته با کدهای html قالب سایت قاطی شده و کل قالب رو بهم بریزه !

ناچارا باید از تابعی استفاده شود که کدهای html رشته رو حذف کرده و طول مناسبی از اون رو برگردونه !

با کمی جستجو به کد زیر رسیدم :



private string HtmlTrimmer(string input, int len)
{
if (string.IsNullOrEmpty(input))
return string.Empty;
if (input.Length <= len)
return input;
// this is necissary because regex "^" applies to the start of the string, not where you tell it to start from
string inputCopy;
string tag;
string result = "";
int strLen = 0;
int strMarker = 0;
int inputLength = input.Length;
Stack stack = new Stack(10);
Regex text = new Regex("^[^<&]+");
Regex singleUseTag = new Regex("^<[^>]*?/>");
Regex specChar = new Regex("^&[^;]*?;");
Regex htmlTag = new Regex("^<.*?>");
while (strLen < len)
{
inputCopy = input.Substring(strMarker);
//If the marker is at the end of the string OR
//the sum of the remaining characters and those analyzed is less then the maxlength
if (strMarker >= inputLength || (inputLength - strMarker) + strLen < len)
break;
//Match regular text
result += text.Match(inputCopy,0,len-strLen);
strLen += result.Length - strMarker;
strMarker = result.Length;
inputCopy = input.Substring(strMarker);
if (singleUseTag.IsMatch(inputCopy))
result += singleUseTag.Match(inputCopy);
else if (specChar.IsMatch(inputCopy))
{
//think of as 1 character instead of 5
result += specChar.Match(inputCopy);
++strLen;
}
else if (htmlTag.IsMatch(inputCopy))
{
tag = htmlTag.Match(inputCopy).ToString();
//This only works if this is valid Markup...
if(tag[1]=='/') //Closing tag
stack.Pop();
else //not a closing tag
stack.Push(tag);
result += tag;
}
else //Bad syntax
result += input[strMarker];
strMarker = result.Length;
}
while (stack.Count > 0)
{
tag = stack.Pop().ToString();
result += tag.Insert(1, "/");
}
if (strLen == len)
result += "...";
return result;
}

ولی وقتی اونو به vb.net تبدیل میکنم ، کار نمیکنه !

البته نمیدونم که c# اون کار میکنه یا نه ! ولی vb.net که کار نکرد !

اینم کد vb.net :


Private Function HtmlTrimmer(ByVal input As String, ByVal len As Integer) As String
If String.IsNullOrEmpty(input) Then
Return String.Empty
End If
If input.Length <= len Then
Return input
End If

' this is necissary because regex "^" applies to the start of the string, not where you tell it to start from
Dim inputCopy As String
Dim tag As String

Dim result As String = ""
Dim strLen As Integer = 0
Dim strMarker As Integer = 0
Dim inputLength As Integer = input.Length

Dim stack As New Stack(10)
Dim text As New Regex("^[^<&]+")
Dim singleUseTag As New Regex("^<[^>]*?/>")
Dim specChar As New Regex("^&[^;]*?;")
Dim htmlTag As New Regex("^<.*?>")

While strLen < len
inputCopy = input.Substring(strMarker)
'If the marker is at the end of the string OR
'the sum of the remaining characters and those analyzed is less then the maxlength
If strMarker >= inputLength OrElse (inputLength - strMarker) + strLen < len Then
Exit While
End If

'Match regular text
result += text.Match(inputCopy, 0, len - strLen)
strLen += result.Length - strMarker
strMarker = result.Length

inputCopy = input.Substring(strMarker)
If singleUseTag.IsMatch(inputCopy) Then
result += singleUseTag.Match(inputCopy)
ElseIf specChar.IsMatch(inputCopy) Then
'think of as 1 character instead of 5
result += specChar.Match(inputCopy)
strLen += 1
ElseIf htmlTag.IsMatch(inputCopy) Then
tag = htmlTag.Match(inputCopy).ToString()
'This only works if this is valid Markup...
If tag(1) = "/"c Then
'Closing tag
stack.Pop()
Else
'not a closing tag
stack.Push(tag)
End If
result += tag
Else
'Bad syntax
result += input(strMarker)
End If

strMarker = result.Length
End While

While stack.Count > 0
tag = stack.Pop().ToString()
result += tag.Insert(1, "/")
End While
If strLen = len Then
result += "..."
End If
Return result
End Function


کد محل بروز خطا :



result += text.Match(inputCopy, 0, len - strLen)




result += singleUseTag.Match(inputCopy)




result += specChar.Match(inputCopy)

mahdi_farhani
یک شنبه 05 مهر 1388, 16:22 عصر
یک راه ساده تر ..... زمانی که میخواهی کد HTML رو ذخیره کنی ، کد ساده(Plane) اونو هم Trim کن و ذخیره کن این همه درده سر هم نداری

amir_3d_mad
یک شنبه 05 مهر 1388, 18:17 عصر
یک راه ساده تر ..... زمانی که میخواهی کد HTML رو ذخیره کنی ، کد ساده(Plane) اونو هم Trim کن و ذخیره کن این همه درده سر هم نداری

آخه نمیخوام دوباره کاری بشه و یک اطلاعات رو دوبار ذخیره کنم !

amir_3d_mad
دوشنبه 06 مهر 1388, 15:17 عصر
لطفا یه نگاهی بهش بندازین !

nazaninam
سه شنبه 07 مهر 1388, 08:13 صبح
یک نگاهی به مقاله زیر بنداز ، امیدوارم بدردت بخوره :
http://how2learnasp.net/article.aspx?code=4a0e5bb4-31fe-435f-97f9-088db9b4621d