Code:
Sub LongWayOfDoingIt()
10 Rem 1) Test string
20 Dim strBBCode As String '
30 Let strBBCode = "gggg[d=fg]2[/d]45[/8]x[ddd[Cl=XYZ][/Cl]]Any text"
40 Dim strRmveBBCode As String: Let strRmveBBCode = strBBCode ' This will be adjusted until finally has no BBCode tag pairs
50 Dim Lstr As Long ' This will be adjusted as necerssary for current length ' Long is very simple to handle, - final memory "size" type is known (123.456 and 000.001 have same "size" computer memory ) , and so a Address suggestion can be given for the next line when the variable is filled in. '( Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647) If you need some sort of validation the value should only be within the range of a Byte/Integer otherwise there's no point using anything but Long.--upon/after 32-bit, Integers (Short) need converted internally anyways, so a Long is actually faster. )
60 Let Lstr = Len(strBBCode)
70 Rem 2) Loop through and get a BB Code section
80 Dim strBBCodePair As String ' for a found valid pair '
' Prepares "Pointer" to a "Blue Print" (or Form, Questionaire not yet filled in, a template etc.)"Pigeon Hole" in Memory, sufficient in construction to house a piece of Paper with code text giving the relevant information for the particular Variable Type. VBA is sent to it when it passes it. In a Routine it may be given a particular “Value”, or (“Values” for Objects). There instructions say then how to do that and handle(store) that(those). At Dim the created Paper is like a Blue Print that has some empty spaces not yet filled in. A String is a a bit tricky. The Blue Print code line Paper in the Pigeon Hole will allow to note the string Length and an Initial start memory Location. This Location well have to change frequently as strings of different length are assigned. Instructiions will tell how to do this. Theoretically a specilal value vbNullString is set to aid in quich checks.. But..http://www.mrexcel.com/forum/excel-questions/361246-vbnullstring-2.html#post44116
90 Dim posCurrent As Long 'Current position in the string' Loop Bound variable Count '_-Main Loop==========================
100 For posCurrent = Lstr To 2 Step -1 'Important to go backwards as we chop off behind us so have a less complicated current position. We look at pos-1 in some code parts so we would error if we went back to less than 2
110 '2a) Check for a ] Most likely a end of end tag ' ---Loop To find ]
120 If Mid(strRmveBBCode, posCurrent, 1) = "]" Then
130 Dim posBBCodeTagSrch As Long '
140 Dim BcrdEEnd As Long: Let BcrdEEnd = posCurrent 'Position of possible End Code Tag End ' ---Loop To Find [/--
150 For posBBCodeTagSrch = posCurrent - 1 To 2 Step -1 'PosCurrent is set here at loop start. ###It is not effected by cardinally sining changing PosCurrent Loop Bound variable caount in Outer main Loop
160 If Mid(strRmveBBCode, posBBCodeTagSrch, 1) = "]" Then Let BcrdEEnd = posBBCodeTagSrch: GoTo NxtCESPosPos ' If we hit another ] then change the stop tag stop character position and start again to see if is a valid BB code section
170 If Mid(strRmveBBCode, posBBCodeTagSrch, 1) = "/" And Mid(strRmveBBCode, posBBCodeTagSrch - 1, 1) = "[" Then 'We hit start of a BB Code end tag
180 Dim BcrdESt As Long: Let BcrdESt = posBBCodeTagSrch - 1 'Position of End Code Tag Start
190 Exit For ' We leave this For the End Code Tag Start Search, Exit For with a [ at posBBCodeTagSrch-1
200 Else 'Not found start of end Tag or stop of end Tag.
210 End If
220 NxtCESPosPos: Next posBBCodeTagSrch ' go back in possible BB code string ' ---End Loop to find [/---------------------
230 If posBBCodeTagSrch = 3 Then Exit Sub 'case we never found the start of an End Code Tag
235 '2b) Check for a ] Most Likely end of start tag ' ---Loop to find ]
240 For posBBCodeTagSrch = posBBCodeTagSrch - 2 To 2 Step -1 ' Once again ### the start is set and fixed, the Loop Bound Variable Count will change
250 If Mid(strRmveBBCode, posBBCodeTagSrch, 1) = "]" Then
260 If Mid(strRmveBBCode, posBBCodeTagSrch - 1, 1) = "]" Then GoTo NxtCSSPosPos ' If we hit another ] then start again to see if is a valid BB code start section
270 Dim BcrdSEnd As Long: Let BcrdSEnd = posBBCodeTagSrch 'Position of possible Start Code Tag End
280 Dim LTagStLoop As Long 'Last loop to find start of start Code Tag
290 For LTagStLoop = posBBCodeTagSrch - 1 To 1 Step -1 '_-Most Inner loop_______to find [/____________
300 If Mid(strRmveBBCode, LTagStLoop, 1) = "[" Then
310 Dim BcrdSSt As Long 'Have to put it here, - I may occaisionally have to set it to 0 in next line
320 If Mid(strRmveBBCode, LTagStLoop + 1, 1) = "/" Then Let BcrdEEnd = BcrdSEnd: Let BcrdSEnd = 0: Let BcrdESt = LTagStLoop: Let BcrdSSt = 0: Let posBBCodeTagSrch = LTagStLoop: GoTo NxtCSSPosPos ' Found a second end Code Tag. So change the previous found start and stop , and adjust the Loop Bound variable Count appropriately for a new search for a start Code Tag
330 Let BcrdSSt = LTagStLoop
340 Let posCurrent = BcrdSSt 'End of Code Tag section search
350 GoTo FkOffCrd ' We leave this For the start Code Tag Start Search and go to Fuk Off Crd:
360 Else ' Still looking for start of start Code Tag
370 End If
380 Next LTagStLoop '_- End Most inner loop_____________________to find [/_____________________________
390 If LTagStLoop = 1 Then GoTo TheEnd 'Case I think we failed to find a start start [ for the complete possible Code Tag section search
400 Else 'Not found end of start Tag or start of start Tag
410 End If
420 NxtCSSPosPos: Next posBBCodeTagSrch ' go back in possible BB code string' ---End Loop to find ]
425 If posBBCodeTagSrch = 0 Then GoTo TheEnd ' For any outer search failed to find a ] at all or a search for matching start tag that was fruitless
430 FkOffCrd: Rem 3) "I think we come here with a complete set of 4 positions"
440 If BcrdEEnd <> 0 And BcrdESt <> 0 And BcrdSEnd <> 0 And BcrdSSt <> 0 Then 'Just to check'
450 Dim SttBcrd As String, StpBcrd As String ' Start and stop tags as coplete strings including [ and ]
460 Let SttBcrd = Mid(strRmveBBCode, BcrdSSt, BcrdSEnd - BcrdSSt + 1) ' ' like |Color=Red|
465 Let StpBcrd = Mid(strRmveBBCode, BcrdESt, BcrdEEnd - BcrdESt + 1) ' ' like |/Color|
470 Dim SttWrd As String, StpWrd As String
480 Let StpWrd = Mid(StpBcrd, 3, Len(StpBcrd) - 3) ' like Color from |/Color| - Whole Word, from 3rd Chr , for Length as WholeWord-3
490 Let SttWrd = Mid(SttBcrd, 2, Len(StpWrd)) ' like Color from |Color=Red|
500 If UCase(SttWrd) = UCase(StpWrd) And Mid(StpBcrd, 2, 1) = "/" Then 'Ucase allows for differences in cases And Mid(SttBcrd, Len(SttWrd) + 2, 1) = "=" Then
510 Let strRmveBBCode = Replace(strRmveBBCode, SttBcrd, "", 1, 1) ' Replace in ( strRmveBBCode , the start tag , with no string , start loooking from and return from character 1 )
515 Let strRmveBBCode = Replace(strRmveBBCode, StpBcrd, "", 1, 1)
520 Else 'No shortening of string for non matching tag word
530 End If
540 Else 'No shortening of string for not all 4 [ and ] found. Not sure if I ever come here..
550 End If
560 Else ' no last ] of a possible Code section yet, so keep going back down in the main string , main loop
570 End If
580 Next posCurrent '_-===Main Loop========================================================================================
590 TheEnd:
600 MsgBox Prompt:="" & strRmveBBCode & "": Debug.Print "" & strRmveBBCode & "" 'Hit Ctrl+g when in VB Editor Window, reveal Immediate window after code run
End Sub
Bookmarks