|
-
Apr 7th, 2005, 08:51 AM
#1
Thread Starter
PowerPoster
Question for my VB guru crew!
If you wanted to write an app, to just convert text that is contained between a < and a > from upper to lower case, how would you go about it? Would you use regexp or are there VB methods better suited? Thanks guys.
Last edited by visualAd; Apr 8th, 2005 at 02:00 PM.
-
Apr 7th, 2005, 09:33 AM
#2
Re: Question for my VB guru crew!
Are you trying to convert HTML 4.0 to XHTML? - If so then you can use the w3c's Tidy tool. If you don't want to use that then yes, you would use a regular expression. I haven't got the time right now, but if you want me to write a small PHP script, I'll do it a bit later. Shouldn't take long, I once wrote something very similar which finds and lists all the Urls on a web page.
P.s: why didn't you want to post this in the main forum?
-
Apr 7th, 2005, 10:04 AM
#3
Thread Starter
PowerPoster
Re: Question for my VB guru crew!
Yeah that tool would do it, LOL thanks. I would've enjoyed writing it though, as this job really doesn't ever bring me in contact with VB, and I found VB a lot of fun, what little time I spent with it. It's almost to programming what doing a comedy is to acting. It's just plain more fun.
-
Apr 7th, 2005, 10:05 AM
#4
Retired VBF Adm1nistrator
Re: Question for my VB guru crew!
Am in work at present, but if you give me a few hours I'll write an app for you to do it. Easiest probably;
Split() text by "<"
Loop through array and make letters in string lower case
Join() array by "<"
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Apr 7th, 2005, 10:17 AM
#5
Re: Question for my VB guru crew!
VB Code:
Option Explicit
Private Const MY_DATA As String = "<b>Woof</b><div>Moose</div>"
Private Sub Command1_Click()
MsgBox ChangeCase(MY_DATA, True)
End Sub
Private Function ChangeCase(ByVal pstrMyData As String, ByVal pblnUpperCase As Boolean) As String
Dim lngStart As Long
Dim lngEnd As Long
Dim strMyData As String
Dim strText As String
strMyData = pstrMyData
Do
lngStart = InStr(lngEnd + 1, strMyData, "<")
If lngStart > 0 Then
lngEnd = InStr(lngStart + 1, strMyData, ">")
If lngEnd > 0 Then
strText = Mid$(strMyData, lngStart, lngEnd - lngStart)
If pblnUpperCase Then
strText = UCase$(strText)
Else
strText = LCase$(strText)
End If
Mid$(strMyData, lngStart, lngEnd - lngStart) = strText
End If
End If
Loop Until (lngStart = 0 Or lngEnd = 0)
ChangeCase = strMyData
End Function
Hope that help.
Woof
-
Apr 7th, 2005, 10:18 AM
#6
Re: Question for my VB guru crew!
VB Code:
Dim intStart As Integer
Dim IntEnd As Integer
Const TEST_DATA As String = "ThiS <iS a SIMPLE> A tEst"
intStart = InStr(TEST_DATA, "<")
IntEnd = InStr(TEST_DATA, ">")
MsgBox LCase(Mid$(TEST_DATA, intStart + 1, IntEnd - intStart - 1))
Jamie, I don't think Split would work in this case because how would you know which piece of the split output contained the "<" to begin with?
-
Apr 7th, 2005, 10:30 AM
#7
Re: Question for my VB guru crew!
Marty, where's your looping in case there is more than 1 set of <>'s?

Woka
-
Apr 7th, 2005, 10:33 AM
#8
Re: Question for my VB guru crew!
If that's what the data requires then you're absolutely right.
-
Apr 7th, 2005, 11:53 AM
#9
Thread Starter
PowerPoster
Re: Question for my VB guru crew!
Hey thanks all, but the tool that was mentioned would do it fine. I just wanted the fun of having a reason to write something in VB. I stink at coming up with my own reasons to. Also tough to find the time when you can't convince yourself it's really necessary.
-
Apr 7th, 2005, 12:28 PM
#10
Re: Question for my VB guru crew!
 Originally Posted by JPnyc
Hey thanks all, but the tool that was mentioned would do it fine. I just wanted the fun of having a reason to write something in VB. I stink at coming up with my own reasons to. Also tough to find the time when you can't convince yourself it's really necessary. 
Sorry to spoil your fun - But I guess if you prefer a GUI then it is something that can be done in VB.
You'll notice on the tidy site that someone has compiled a dll with the tidy API, so one could use this library and a few API calls to make an application in VB.
-
Apr 8th, 2005, 09:41 AM
#11
Retired VBF Adm1nistrator
Re: Question for my VB guru crew!
 Originally Posted by MartinLiss
VB Code:
Dim intStart As Integer
Dim IntEnd As Integer
Const TEST_DATA As String = "ThiS <iS a SIMPLE> A tEst"
intStart = InStr(TEST_DATA, "<")
IntEnd = InStr(TEST_DATA, ">")
MsgBox LCase(Mid$(TEST_DATA, intStart + 1, IntEnd - intStart - 1))
Jamie, I don't think Split would work in this case because how would you know which piece of the split output contained the "<" to begin with?
Try it 
VB Code:
Dim strText As String = "blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> "
Dim strArr() As String = Split(strText, "<")
Dim i As Integer
For i = 0 To UBound(strArr)
strArr(i) = LCase(strArr(i))
Next
MsgBox(Join(strArr, "<"))
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Apr 8th, 2005, 10:35 AM
#12
Thread Starter
PowerPoster
Re: Question for my VB guru crew!
That's some really efficient code there man, I'm impressed. I think I will give that a try, soon as I load Visual Studio on this PC (it's brand spanking new). Grazie!
-
Apr 8th, 2005, 11:41 AM
#13
Re: Question for my VB guru crew!
 Originally Posted by plenderj
Try it
VB Code:
Dim strText As String = "blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> "
Dim strArr() As String = Split(strText, "<")
Dim i As Integer
For i = 0 To UBound(strArr)
strArr(i) = LCase(strArr(i))
Next
MsgBox(Join(strArr, "<"))
Ignoring the fact that your Dim statements are invalid , my point which I apparently didn't make was that unless you know where the "<" symbols were to begin with, your code would change everything to lower case so you might as well just do
strText = LCase(strText)
-
Apr 8th, 2005, 11:53 AM
#14
Re: Question for my VB guru crew!
In XHTML the only parts of the tag which must be lower case are the tags names and attributes, hence:
HTML Code:
<A HREF="value" ID="value" TABORDER=1>
Must change to:
HTML Code:
<a href="value" id="value" taborder="1">
All attribute vlaues must also be in quotes
-
Apr 8th, 2005, 01:07 PM
#15
Re: Question for my VB guru crew!
I'm sure someone could change this to make it shorter. - but it does what the previous post says it should.
VB Code:
Function LCaseTags(ByVal str)
Dim InTag As Boolean
Dim InAttributeValue As Boolean
Dim LookForSpace As Boolean
Dim IntLen As Long
Dim x As Long
Dim StrReturn As String
Dim Char As String * 1
IntLen = Len(str)
InTag = False
InAtrributeValue = False
LookForSpace = False
nextchar = vbNullString
For x = 1 To IntLen
Char = Mid(str, x, 1)
If Char = "<" Then
InTag = True
ElseIf Char = ">" Then
If LookForSpace Then
StrReturn = StrReturn & """"
LookForSpace = False
InAttributeValue = False
End If
InTag = False
ElseIf InTag Then
If nextchar <> vbNullString And Char <> nextchar Then
StrReturn = StrReturn & """"
nextchar = vbNullString
LookForSpace = True
InAttributeValue = True
End If
If LookForSpace And IsSpace(Char) Then
InAttributeValue = False
LookForSpace = False
StrReturn = StrReturn & """"
ElseIf Char = """" Then
If InAttributeValue Then
InAttributeValue = False
Else
InAttributeValue = True
End If
ElseIf Char = "=" Then
nextchar = """"
ElseIf IsUpper(Char) Then
If Not InAttributeValue Then
Char = LCase(Char)
End If
End If
End If
StrReturn = StrReturn & Char
Next x
LCaseTags = StrReturn
End Function
Function IsUpper(ByVal str As String) As Boolean
str = Left$(str, 1)
IsUpper = (Asc(str) >= Asc("A")) And (Asc(str) <= Asc("Z"))
End Function
Function IsSpace(ByVal str As String) As Boolean
Dim whitespace(4) As String
Dim x As Integer
whitespace(0) = " "
whitespace(1) = vbCr
whitespace(2) = vbLf
whitespace(2) = Chr(9)
For x = 0 To 2
If whitespace(x) = str Then
IsSpace = True
Exit Function
End If
Next x
IsSpace = False
End Function
-
Apr 8th, 2005, 01:43 PM
#16
Re: Question for my VB guru crew!
Jamie...your code changes all text to lower case. ie:
Would be changed too:
VisualAd...yea it can be done shorter...look at my code 
Woka
-
Apr 8th, 2005, 01:55 PM
#17
Thread Starter
PowerPoster
Re: Question for my VB guru crew!
I believe in XHTML that tag names, attr. names and values must all be lower case. The validator kicks back BORDER but not border.
-
Apr 8th, 2005, 02:04 PM
#18
Re: Question for my VB guru crew!
Yes, that and all single tags such as IMG an BR must have a terminator. IMG tags are also required to have an alt attribute. All these and the other other differences are taken care of by the tidy tool.
HTML Code:
<img src="img.gif" alt="Alternate text" />
<br />
-
Apr 8th, 2005, 02:05 PM
#19
Re: Question for my VB guru crew!
I agree.
Jamie...your code:
VB Code:
Dim strText As String = "blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> "
Dim strArr() As String = Split(strText, "<")
Dim i As Integer
For i = 0 To UBound(strArr)
strArr(i) = LCase(strArr(i))
Next
MsgBox(Join(strArr, "<"))
does exactly the same thing as:
VB Code:
Dim strText As String = "blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> "
strText = strText.ToLower
Since this is .NET u shouldn't use LCase...
WOka
-
Apr 8th, 2005, 02:07 PM
#20
Re: Question for my VB guru crew!
Boooo...just realised my code changes:
To
I suck.
This isn't a problem on image tags but it is for certain others. Booooo.
Woka
-
Apr 8th, 2005, 02:30 PM
#21
Re: Question for my VB guru crew!
The code I wrote does the following:
- Converts all tag names and attribute names to lower case.
- Ensures attribute values are between dowuble quotes and adds them if they are not.
- Takes into account white spcae such as tabs spaces an new lines.
So it will convert:
HTML Code:
<A HREF="value"
naMe=VALUE
>HELLO
How are you</A>
To:
HTML Code:
<a href="value"
name="VALUE"
>HELLO
How are you</a>
-
Apr 9th, 2005, 04:59 AM
#22
Retired VBF Adm1nistrator
Re: Question for my VB guru crew!
 Originally Posted by MartinLiss
Ignoring the fact that your Dim statements are invalid  , my point which I apparently didn't make was that unless you know where the "<" symbols were to begin with, your code would change everything to lower case so you might as well just do
strText = LCase(strText)
I coded that in VB.NET 
Okay point taken... but that could be remedied by Split()'ing each array index using > and then modifying that...
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|