Results 1 to 22 of 22

Thread: Question for my VB guru crew!

  1. #1

    Thread Starter
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015

    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.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015

    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.

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    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]

  5. #5
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Question for my VB guru crew!

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const MY_DATA As String = "<b>Woof</b><div>Moose</div>"
    4.  
    5. Private Sub Command1_Click()
    6.     MsgBox ChangeCase(MY_DATA, True)
    7. End Sub
    8.  
    9. Private Function ChangeCase(ByVal pstrMyData As String, ByVal pblnUpperCase As Boolean) As String
    10. Dim lngStart    As Long
    11. Dim lngEnd      As Long
    12. Dim strMyData   As String
    13. Dim strText     As String
    14.     strMyData = pstrMyData
    15.     Do
    16.         lngStart = InStr(lngEnd + 1, strMyData, "<")
    17.         If lngStart > 0 Then
    18.             lngEnd = InStr(lngStart + 1, strMyData, ">")
    19.             If lngEnd > 0 Then
    20.                 strText = Mid$(strMyData, lngStart, lngEnd - lngStart)
    21.                 If pblnUpperCase Then
    22.                     strText = UCase$(strText)
    23.                 Else
    24.                     strText = LCase$(strText)
    25.                 End If
    26.                 Mid$(strMyData, lngStart, lngEnd - lngStart) = strText
    27.             End If
    28.         End If
    29.     Loop Until (lngStart = 0 Or lngEnd = 0)
    30.     ChangeCase = strMyData
    31. End Function
    Hope that help.

    Woof

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Question for my VB guru crew!

    VB Code:
    1. Dim intStart As Integer
    2.     Dim IntEnd As Integer
    3.     Const TEST_DATA As String = "ThiS <iS a SIMPLE> A tEst"
    4.    
    5.     intStart = InStr(TEST_DATA, "<")
    6.     IntEnd = InStr(TEST_DATA, ">")
    7.    
    8.     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?

  7. #7

  8. #8

  9. #9

    Thread Starter
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015

    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.

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Question for my VB guru crew!

    Quote 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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: Question for my VB guru crew!

    Quote Originally Posted by MartinLiss
    VB Code:
    1. Dim intStart As Integer
    2.     Dim IntEnd As Integer
    3.     Const TEST_DATA As String = "ThiS <iS a SIMPLE> A tEst"
    4.    
    5.     intStart = InStr(TEST_DATA, "<")
    6.     IntEnd = InStr(TEST_DATA, ">")
    7.    
    8.     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:
    1. Dim strText As String = "blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> "
    2.         Dim strArr() As String = Split(strText, "<")
    3.         Dim i As Integer
    4.         For i = 0 To UBound(strArr)
    5.             strArr(i) = LCase(strArr(i))
    6.         Next
    7.         MsgBox(Join(strArr, "<"))
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  12. #12

    Thread Starter
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015

    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!

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Question for my VB guru crew!

    Quote Originally Posted by plenderj
    Try it

    VB Code:
    1. Dim strText As String = "blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> "
    2.         Dim strArr() As String = Split(strText, "<")
    3.         Dim i As Integer
    4.         For i = 0 To UBound(strArr)
    5.             strArr(i) = LCase(strArr(i))
    6.         Next
    7.         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)

  14. #14
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  15. #15
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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:
    1. Function LCaseTags(ByVal str)
    2.     Dim InTag As Boolean
    3.     Dim InAttributeValue As Boolean
    4.     Dim LookForSpace As Boolean
    5.    
    6.     Dim IntLen As Long
    7.    
    8.     Dim x As Long
    9.    
    10.     Dim StrReturn As String
    11.     Dim Char As String * 1
    12.    
    13.     IntLen = Len(str)
    14.    
    15.     InTag = False
    16.     InAtrributeValue = False
    17.     LookForSpace = False
    18.     nextchar = vbNullString
    19.    
    20.     For x = 1 To IntLen
    21.         Char = Mid(str, x, 1)
    22.        
    23.         If Char = "<" Then
    24.             InTag = True
    25.         ElseIf Char = ">" Then
    26.             If LookForSpace Then
    27.                 StrReturn = StrReturn & """"
    28.                
    29.                 LookForSpace = False
    30.                 InAttributeValue = False
    31.             End If
    32.            
    33.             InTag = False
    34.         ElseIf InTag Then
    35.             If nextchar <> vbNullString And Char <> nextchar Then
    36.                 StrReturn = StrReturn & """"
    37.                 nextchar = vbNullString
    38.                
    39.                 LookForSpace = True
    40.                 InAttributeValue = True
    41.             End If
    42.            
    43.             If LookForSpace And IsSpace(Char) Then
    44.                 InAttributeValue = False
    45.                 LookForSpace = False
    46.                
    47.                 StrReturn = StrReturn & """"
    48.             ElseIf Char = """" Then
    49.                 If InAttributeValue Then
    50.                     InAttributeValue = False
    51.                 Else
    52.                     InAttributeValue = True
    53.                 End If
    54.             ElseIf Char = "=" Then
    55.                 nextchar = """"
    56.             ElseIf IsUpper(Char) Then
    57.                 If Not InAttributeValue Then
    58.                     Char = LCase(Char)
    59.                 End If
    60.             End If
    61.         End If
    62.        
    63.         StrReturn = StrReturn & Char
    64.     Next x
    65.    
    66.     LCaseTags = StrReturn
    67. End Function
    68.  
    69. Function IsUpper(ByVal str As String) As Boolean
    70.     str = Left$(str, 1)
    71.    
    72.     IsUpper = (Asc(str) >= Asc("A")) And (Asc(str) <= Asc("Z"))
    73. End Function
    74.  
    75. Function IsSpace(ByVal str As String) As Boolean
    76.     Dim whitespace(4) As String
    77.     Dim x As Integer
    78.  
    79.     whitespace(0) = " "
    80.     whitespace(1) = vbCr
    81.     whitespace(2) = vbLf
    82.     whitespace(2) = Chr(9)
    83.    
    84.     For x = 0 To 2
    85.         If whitespace(x) = str Then
    86.             IsSpace = True
    87.            
    88.             Exit Function
    89.         End If
    90.     Next x
    91.    
    92.     IsSpace = False
    93. End Function
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  16. #16
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Question for my VB guru crew!

    Jamie...your code changes all text to lower case. ie:
    Code:
    <B>WOOF</B>
    Would be changed too:
    Code:
    <b>woof</b>
    VisualAd...yea it can be done shorter...look at my code

    Woka

  17. #17

    Thread Starter
    PowerPoster JPnyc's Avatar
    Join Date
    Oct 2002
    Location
    Manhattan
    Posts
    3,015

    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.

  18. #18
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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 />
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  19. #19
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Question for my VB guru crew!

    I agree.

    Jamie...your code:
    VB Code:
    1. Dim strText As String = "blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> "
    2.         Dim strArr() As String = Split(strText, "<")
    3.         Dim i As Integer
    4.         For i = 0 To UBound(strArr)
    5.             strArr(i) = LCase(strArr(i))
    6.         Next
    7.         MsgBox(Join(strArr, "<"))
    does exactly the same thing as:
    VB Code:
    1. Dim strText As String = "blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> blah <sOmEtAg>blah></someTAg> "
    2.         strText = strText.ToLower
    Since this is .NET u shouldn't use LCase...

    WOka

  20. #20
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Question for my VB guru crew!

    Boooo...just realised my code changes:
    Code:
    <img src="Img.GIF"
    To
    Code:
    <img src="img.gif"
    I suck.
    This isn't a problem on image tags but it is for certain others. Booooo.

    Woka

  21. #21
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  22. #22
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: Question for my VB guru crew!

    Quote 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
  •  



Click Here to Expand Forum to Full Width