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.
Printable View
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.
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? :confused:
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.
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 "<"
Hope that help.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
Woof
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?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))
Marty, where's your looping in case there is more than 1 set of <>'s?
:D
Woka
If that's what the data requires then you're absolutely right.
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. :bigyello:
Sorry to spoil your fun ;) - But I guess if you prefer a GUI then it is something that can be done in VB.Quote:
Originally Posted by JPnyc
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.
Try it :)Quote:
Originally Posted by MartinLiss
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, "<"))
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!
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 doQuote:
Originally Posted by plenderj
strText = LCase(strText)
In XHTML the only parts of the tag which must be lower case are the tags names and attributes, hence:
Must change to:HTML Code:<A HREF="value" ID="value" TABORDER=1>
All attribute vlaues must also be in quotes ;)HTML Code:<a href="value" id="value" taborder="1">
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
Jamie...your code changes all text to lower case. ie:
Would be changed too:Code:<B>WOOF</B>
VisualAd...yea it can be done shorter...look at my code ;)Code:<b>woof</b>
Woka
I believe in XHTML that tag names, attr. names and values must all be lower case. The validator kicks back BORDER but not border.
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 />
I agree.
Jamie...your code:
does exactly the same thing as: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, "<"))
Since this is .NET u shouldn't use LCase...;)VB Code:
Dim strText As String = "blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> " strText = strText.ToLower
WOka
Boooo...just realised my code changes:
ToCode:<img src="Img.GIF"
I suck.Code:<img src="img.gif"
This isn't a problem on image tags but it is for certain others. Booooo.
Woka
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:
To:HTML Code:<A HREF="value"
naMe=VALUE
>HELLO
How are you</A>
HTML Code:<a href="value"
name="VALUE"
>HELLO
How are you</a>
I coded that in VB.NET ;)Quote:
Originally Posted by MartinLiss
Okay point taken... but that could be remedied by Split()'ing each array index using > and then modifying that...