I need to be able to find some way of formatting HTML text. For example :
Suppose I have some formatted text, such as : <STRONG>Hello, My name is <EM><FONT Color=red>MARKO</FONT></EM></STRONG> and I need to be able to put in a <EM><STRONG>line break</STRONG></EM> which is a <STRONG>'br'</STRONG> tag whenever the displayed texts count reaches say 50
The innerhtml text of this is :
Hello, My name is MARKO and I need to be able to put in a line break which is a 'br' tag whenever the displayed texts count reaches say 50
As you can see, character 50 in the text is the 'u' of put as in 'put a line break'
I need to be able to iterate through the formatted text string ignoring the Tags and inserting a <br> tag at every 50th character. This is not practical to put in a br tag at the u of put here as this will break the word up. and so I will need to see where the first available space is - be it before the word or after - and then insert a br tag.
Can anybody point me in the right direction?
thanks
Marko.
PeOfEo
12-03-2003, 02:20 PM
well you can count out 50 characters using the mid statement and then run an if statement to see if it is a space or a letter, um maybe you could try getting the value of 50, if its a letter move on to the next character etc and run it in a loop and then put it into an if statement. I do not use asp classic so I do not know the syntax of these string statements off the top of my head so if you want it in classic Ill have to get back to you.
marko_one
12-03-2003, 04:58 PM
Sorry PeOfEo, I may not have explained my requirement too well.
I need to be able to keep the formatting intact <strong>, <em>, <U> etc. When the text is displayed, you don't see the tags as in : Bold, italic, underlined text, you see that as formatted bold, underlined italic text but you dont see the <b>, <u> and <i> tags. They are still in the html tagged text though. What I need to do is extract the text without the tags and insert a <br> tag back into the formatted text, so from my original text :
<STRONG>Hello, My name is <EM><FONT Color=red>MARKO</FONT></EM></STRONG> and I need to be able to put in a <EM><STRONG>line break</STRONG></EM> which is a <STRONG>'br'</STRONG> tag whenever the displayed texts count reaches say 50
this will have a <br> tag inserted at the space after the 'put' string, so the new text will be :
<STRONG>Hello, My name is <EM><FONT Color=red>MARKO</FONT></EM></STRONG> and I need to be able to put<br> in a <EM><STRONG>line break</STRONG></EM> which is a <STRONG>'br'</STRONG> tag whenever the displayed texts count reaches say 50
So it's a bit more complicated than just counting through and inserting a <br> tag at character 50 if you see what i mean??
Marko
PeOfEo
12-04-2003, 12:00 AM
does not change what I said. It would keep the text formatted. When it sees a character it moves on. This would not break the formated text, I am missing your problem now.
MichaelM
12-04-2003, 03:18 PM
I scratched the following out and it works, it isn't commented but I tried to use verbose variable names to help explain the thought process. The main downfall of the following code is that I didn't take the time to write a proper algorithm in the For Loop of the InsertLineBreaks function so that it could handle strings of more than 149 plain text characters. Anyway, hope this helps:
<%
Option Explicit
%>
<html>
<head>
<title>Formatted Text with BR's</title>
</head>
<body>
<font face="Courier">
<%
Dim counter, indexes, values
Response.Write InsertLineBreaks("<STRONG>Hello, My name is <EM><FONT Color=red>MARKO</FONT></EM></STRONG> and I need to be able to put in a <EM><STRONG>line break</STRONG></EM> which is a <STRONG>'br'</STRONG> tag whenever the displayed texts count reaches say 50")
Function InsertLineBreaks(strFormattedText)
Dim strPlainText, iterate, i
strPlainText = StripOutHTML(strFormattedText)
strPlainText = getLineBreaks(strPlainText)
iterate = 0
If indexes(iterate) = 0 Then
strPlainText = values(iterate) & strPlainText
iterate = 1
End If
For i = iterate To Ubound(indexes) - 1
if indexes(i) - 50 >= 50 Then indexes(i) = indexes(i) + 4
if indexes(i) - 100 >= 100 Then indexes(i) = indexes(i) + 8
strPlainText = strPlainText & values(i)
InsertLineBreaks = strPlainText
End Function
Function StripOutHTML(strFormattedText)
Dim re, Matches, strTemp, m, x, y
Set re = new RegExp
re.Pattern = "<\/{0,1}\w*[\s*\w*=\'*\""*.*\""*\'*]*>"
re.IgnoreCase = true
re.Global = true
Set Matches = re.Execute(strFormattedText)
strTemp = strFormattedText
For Each m in Matches
strTemp = Replace(strTemp, m.Value, "")
counter = counter + 1
x = x & CStr(m.FirstIndex) & ","
y = y & CStr(m.value) & ","
Next
indexes = Split(x, ",")
values = Split(y, ",")
StripOutHTML = strTemp
Set Matches = Nothing
Set re = Nothing
End Function
Function getLineBreaks(l_strPlainText)
Dim a, i, segment, words, n, w
If Len(l_strPlainText) > 50 Then
a = getArray(l_strPlainText)
l_strPlainText = ""
For i=0 To UBound(a)-1
segment = a(i)
If Right(segment, 1) <> " " Then
words = Split(segment, " ")
If Len(segment) > 49 Then
words(Ubound(words)-1) = words(Ubound(words)-1) & "<BR>"
Else
words(UBound(words)-1) = words(Ubound(words)-1)
End If
For n=0 To UBound(words)
w = w & words(n) & " "
Next
l_strPlainText = l_strPlainText & Left(w, Len(w) - 1)
w = ""
End If
Next
End If
getLineBreaks = l_strPlainText
End Function
Function getArray(str)
Dim length, final_segment_length, number_of_50_char_segments, num_of_segments, i, x
length = Len(str)
final_segment_length = length Mod 50
number_of_50_char_segments = (length - final_segment_length)/50
num_of_segments = number_of_50_char_segments
If final_segment_length > 0 Then
num_of_segments = num_of_segments + 1
End If
For i = 0 To num_of_segments
x = x & Mid(str, (i*50)+1, 50) & "[*!@!*]"
Next
x = Left(x, Len(x) - 7)
getArray = Split(x, "[*!@!*]")
End Function
%>
</font>
</body>
</html>
PeOfEo
12-05-2003, 07:36 PM
heh that basically does what I was proposting. I did not have time to write anything because I just put my comp backtogehter. I got a new processor and a new motherboard and Moved to a new hd since you cant move xp to a new chipset, and I have all of my files on the old hard drive, ill leave them there for a month then when I am convinced I have it all ill format the old one. :D
marko_one
12-16-2003, 08:34 AM
Thanks for your help, MichaelM and PeOfEo. I am actually doing this coding using VB.NET, but a VBSCRIPT function I'm sure will do it. I created a VB class and so you can instantiate an object of type stringFormat (for example) and pass in a string, with a count of the number of raw characters you need to display before a <br> is injected.
It is kind of a bit long winded and could probably be made much more efficient??
Here is my finished code which works perfectly, for any size string :
'############################
Protected originalString As String
Protected mainStringArray() As String
Protected regExpr As String = "(<[^>]*>)"
Public Function getFormat(ByVal mainstring As String, ByVal count As Integer) As String
splitString(mainstring, regExpr)
addBreaks(count)
Return getString()
End Function
Private Function getString() As String
Dim myString As String = ""
Dim i As Integer = 0
For i = 0 To mainStringArray.Length - 1
myString &= mainStringArray(i)
Next
Return myString
End Function
Private Sub splitString(ByVal mainstring As String, ByVal regExpr As String)
Dim myRegEx As Regex = New Regex(regExpr)
mainStringArray = myRegEx.Split(mainstring, "(<[^>]*>)")
Dim alMainString As New ArrayList
Dim i As Integer
For i = 0 To mainStringArray.Length - 1
If mainStringArray(i) <> "" Then
alMainString.Add(mainStringArray(i))
End If
Next
ReDim mainStringArray(alMainString.Count - 1)
alMainString.CopyTo(mainStringArray)
End Sub
Private Sub addBreaks(ByVal length As Integer)
Dim myRegEx As Regex = New Regex(regExpr)
Dim strCount As Integer = 0
Dim i As Integer
Dim spacePoint As Integer = 0
Dim Spaces As Boolean = True
Dim lenghtAfterSpace As Integer = 0
For i = 0 To mainStringArray.Length - 1
If myRegEx.IsMatch(mainStringArray(i), regExpr) Then ' it's an HTML tag
If UCase(mainStringArray(i)) = "<BR>" Then
strCount = 0
End If
Else 'it's not an HTML tag, its a string, so split that substring into separate words
If strCount + Len(mainStringArray(i)) >= length Then 'we need to add 1 or more <br> tags
Dim j As Integer
Dim substringarray() = Split(mainStringArray(i), " ")
For j = 0 To substringarray.Length - 1
If strCount + Len(substringarray(j)) + 1 >= length Then 'the substring array strips out the space, so add 1
substringarray(j) = "<BR>" & substringarray(j)
strCount = 0
Else
strCount += Len(substringarray(j))
End If
Next
mainStringArray(i) = ""
For j = 0 To substringarray.Length - 1
If j < substringarray.Length - 1 Then 'Make sure we are in the bounds of the array
'If the next part of the arrays' start is a <BR>, then we don't want a space at the end of the current line
If InStr(substringarray(j + 1), "<BR>", CompareMethod.Text) Then
mainStringArray(i) &= substringarray(j)
Else
mainStringArray(i) &= substringarray(j) & " "
End If
Else
mainStringArray(i) &= substringarray(j) & " "
End If
Next
mainStringArray(i) = Trim(mainStringArray(i))
Else
strCount += Len(mainStringArray(i))
End If
End If
Next
End Sub
PeOfEo
12-16-2003, 07:39 PM
I was going to make it in vb.net a while back, but never mind, if this works for yah :D
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.