SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Good at ASP?

  1. #1
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Good at ASP?

    I recently released a product for my e4ums site had a kind of BBCode, for which I wrote a function. Just for fun, I thought I would challenge the lil ASP people to make a function that uses the information in this array:

    doh, it processed the code, lol, www.oreosnbeer.com/array.txt

    Remember to take into account that sometimes people might not want close a tag, so we want it to NOT process the broken code.

    Have fun and remember to show us it.

    Last edited by dhtmlgod; Nov 22, 2001 at 10:56.

  2. #2
    SitePoint Wizard iTec's Avatar
    Join Date
    May 2001
    Location
    Sydney, Australia
    Posts
    2,243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay, i am probably 1000 kays in the middle of knowhere with this one, (not even sure i quite understood the question) but anyway ill shut my mouth and show my code.... AND DONT LAUGH!!
    [edit]VB was messing it up, so i have reversed the [] so they now look like ][
    Code:
    i cut it.... was to damn annoying (pm me if you wanna see it)
    Last edited by iTec; Nov 23, 2001 at 08:44.

  3. #3
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Woah! Big!

    I get an error, you've left out some end if's and a next i think

  4. #4
    SitePoint Wizard iTec's Avatar
    Join Date
    May 2001
    Location
    Sydney, Australia
    Posts
    2,243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yer, i hadnt tested it, i just did it quickly to see if that was what you were after... or if i was anywhere near right. DOH... i deleted the bottom half, thats where i went wrong...gimme 5 D
    Last edited by iTec; Nov 22, 2001 at 12:15.

  5. #5
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just wanted to see how other people would do it.

    Call it nosey, after a lil while I'll post mine, which is 30 lines. hehe


  6. #6
    SitePoint Wizard iTec's Avatar
    Join Date
    May 2001
    Location
    Sydney, Australia
    Posts
    2,243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mines nearly 30... Ive changed it, should HOPEFULLY work now.... i think

  7. #7
    SitePoint Evangelist Umair.ms's Avatar
    Join Date
    Sep 2000
    Location
    Lahore, Pakistan
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    text2Parse = "Appears in bold and this one is [ul]underlined[/ul]. What's a [blockquote]blockquote[/blockquote]?"

    ' Code Starts Here
    Set objRegExp = New RegExp

    With objRegExp
    .IgnoreCase = True
    .Global = False
    .MultiLine = True

    For i = LBound(parserStrs) To UBound(parserStrs)
    .Pattern = "(" & Replace(parserStrs(i)(0), "[", "\[") & ")([\w\W]*)" & "(" & Replace(parserStrs(i)(2), "[", "\[") & ")"
    text2Parse = .Replace(text2Parse, parserStrs(i)(1) & "$2" & parserStrs(i)(3))
    Next
    End With

    Set objRegExp = Nothing
    ' Code Ends Here

    Response.Write text2Parse

    ---

    Smaller code but not perfect code. The problem is that the regular expression (i dont know how to write them in the best possible manner) will match unless it encounters the last of the matching closing tag.

    for example Umair is the [/b]Best. Will yield <b>Umair[/b] is the </b>Best.

    We can try to improve this code.
    TinyPlanet.org
    Discuss and Debate World Events, Politics and Religion.
    Interact and Share your Views with People around the Globe.

  8. #8
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Newcastle, England
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think for regular expressions, your pattern should be along the lines of

    Code:
    opening-delimiter((.|\n)*?)closing-delimiter
    e.g.
    objRegExp.Pattern = "[b]((.|\n)*?)[/b]"
    This will search for the mimimum number of characters between the two tags. Your replacement would obviously then be along the lines of

    Code:
    new-opening-delimiter$1new-closing-delimiter
    e.g.
    strText = objRegExp.Replace(strText, "<b>$1</b>")
    This method would change this:

    [b]Hello[/b] there[/b]

    into:

    <b>Hello</b> there[/b]

    Obviously you could easily perform this in a loop for each tag you have.

    Hope that helps
    Nick Wilson [ - email - ]

  9. #9
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh, the joys of regexp.

    Unlucky for me I don't use them, heres my code as promised:
    Code:
    function parseCode(parsingStr)
    		Do Until IDx = uBound(parserStrs) + 1
    		If inStr(lCase(parsingStr),parserStrs(IDx)(0)) + inStr(lCase(parsingStr),parserStrs(IDx)(2)) > 0  AND inStr(lCase(parsingStr),parserStrs(IDx)(0)) < inStr(lCase(parsingStr),parserStrs(IDx)(2)) then
    			count = inStr(lCase(parsingStr),parserStrs(IDx)(0)) + inStr(lCase(parsingStr),parserStrs(IDx)(2)) 
    			Do Until count = 0 OR count = inStr(lCase(parsingStr),parserStrs(IDx)(0)) OR count = inStr(lCase(parsingStr),parserStrs(IDx)(2)) 
    				startPos = inStr(lCase(parsingStr), parserStrs(IDx)(0))
    				endPos = inStr(startPos, lCase(parsingStr), parserStrs(IDx)(2)) + len(parserStrs(IDx)(2)) - 1
    				test = mid(parsingStr, startPos, endPos)
    				test2 = replace(test,parserStrs(IDx)(0), parserStrs(IDx)(1),1,1,1)
    				test2 = replace(test2,parserStrs(IDx)(2), parserStrs(IDx)(3),1,1,1)
    				parsingStr = replace(parsingStr,test, test2,1,1,1)
    				count = 0
    				count = inStr(parsingStr,parserStrs(IDx)(0)) + inStr(parsingStr,parserStrs(IDx)(2))
    			Loop
    		end if
    		IDx = IDx + 1
    	Loop
    	parseCode = smileParse(parsingStr)
    end function
    damn, tried to post the array again, vB screwed it

    Last edited by dhtmlgod; Nov 23, 2001 at 05:14.

  10. #10
    SitePoint Evangelist Umair.ms's Avatar
    Join Date
    Sep 2000
    Location
    Lahore, Pakistan
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    text2Parse = "Appears in bold and this one is [ul]underlined[/ul]. [/b]What's a [blockquote]blockquote[/blockquote]?"

    ' Code Starts Here
    Set objRegExp = New RegExp

    With objRegExp
    .IgnoreCase = True
    .Global = True
    .MultiLine = True

    For i = LBound(parserStrs) To UBound(parserStrs)
    .Pattern = "(" & Replace(parserStrs(i)(0), "[", "\[") & ")((.|\n)*?)" & "(" & Replace(parserStrs(i)(2), "[", "\[") & ")"
    text2Parse = .Replace(text2Parse, parserStrs(i)(1) & "$2" & parserStrs(i)(3))
    Next
    End With

    Set objRegExp = Nothing
    ' Code Ends Here

    Response.Write text2Parse

    ---------

    Works perfect!! Thanks CrazyCrane. Is there any other way to learn more about regular expressions.

    There is a suggestion for you, dhtmlgod. What about keeping the pattern and the replace strings in an array. This way you can include tags for Website and other (a bit) complex tags.
    TinyPlanet.org
    Discuss and Debate World Events, Politics and Religion.
    Interact and Share your Views with People around the Globe.

  11. #11
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umair, I do use and array, click the link in my first post, thats the array i use, well, not the compelete array, the proper one has a lot more.



    This link: http://www.oreosnbeer.com/array.txt

  12. #12
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The following code is client side so a couple of mods will be needed to convert it to server-side.

    It'll deal with url and mail links.

    I don't know how to deal with correct nesting though.

    4GuysFromRolla have got a number of articles on reg ex stuff.

    http://www.4guysfromrolla.com/webtec...ressions.shtml

    Kevins PHP article's also got some decent stuff on RegEx but I think the sections dealing with it are now only available in the book.

    Code:
    <script type="text/vbscript">
    
    Private Function PatternReplace(Text, Patterns)
    	
    	Dim strText
    	Dim strPattern
    	Dim strReplaceStr
    	Dim intPattCnt
    	Dim i
    	Dim objRegEx
    	
    	strText = Text
    	intPattCnt = UBound(Patterns, 1) + 1
    	
    	If intPattCnt > 0 Then
    		
    		Set objRegEx = CreateObject("VBScript.RegExp")
    		
    		objRegEx.MultiLine = True
    		objRegEx.Global = True
    		objRegEx.IgnoreCase = True
    		
    		For i = 0 To intPattCnt - 1
    			strPattern = Patterns(i)(0)
    			strReplaceStr = Patterns(i)(1)
    			
    			If strpattern <> "" Then
    				objRegEx.Pattern = strPattern
    				strText = objRegEx.Replace(strText, strReplaceStr)
    			End If
    		Next
    		
    		Set objRegExp = Nothing
    	End If
    
    	PatternReplace = strText
    
    End Function
    
    Function HTMLEncode(Text)
    	
    	strText = Text
    	strText = Replace(strText, "<", "&lt;")
    	strText = Replace(strText, ">", "&gt;")
    	
    	HTMLEncode = strText
    
    End Function
    
    
    Dim arrPatterns(15) ' Array for style parsing
    
    arrPatterns(0) = Array("\[b\]((.|\n)*?)\[/b\]","<b>$1</b>")
    arrPatterns(1) = Array("\[i\]((.|\n)*?)\[/i\]","<i>$1</i>")
    arrPatterns(2) = Array("\[h1\]((.|\n)*?)\[/h1\]","<h1>$1</h1>")
    arrPatterns(3) = Array("\[h2\]((.|\n)*?)\[/h3\]","<h2>$1</h2>")
    arrPatterns(4) = Array("\[h3\]((.|\n)*?)\[/h3\]","<h3>$1</h3>")
    arrPatterns(5) = Array("\[h4\]((.|\n)*?)\[/h4\]","<h4>$1</h4>")
    arrPatterns(6) = Array("\[h5\]((.|\n)*?)\[/h5\]","<h5>$1</h5>")
    arrPatterns(7) = Array("\[h6\]((.|\n)*?)\[/h6\]","<h6>$1</h6>")
    arrPatterns(8) = Array("\[li\]((.|\n)*?)\[/li\]","<li>$1</li>")
    arrPatterns(9) = Array("\[ol\]((.|\n)*?)\[/ol\]","<ol>$1</ol>")
    arrPatterns(10) = Array("\[quote\]((.|\n)*?)\[/quote\]","<blockquote>$1</blockquote>")
    arrPatterns(11) = Array("\[link=https://([-_./a-zA-Z0-9!&%#?,'=:~]+)]([-_./a-zA-Z0-9 !&%#?,'=:~]+)\[/link]","<a href=""https://$1"">$2</a>")
    arrPatterns(12) = Array("\[link=http://([-_./a-zA-Z0-9!&%#?,'=:~]+)]([-_./a-zA-Z0-9 !&%#?,'=:~]+)\[/link]","<a href=""http://$1"">$2</a>")
    arrPatterns(13) = Array("\[link=([-_./a-zA-Z0-9!&%#?,'=:~]+)]([-_./a-zA-Z0-9 !&%#?,'=:~]+)\[/link]","<a href=""http://$1"">$2</a>")
    arrPatterns(14) = Array("\[link]([-_./a-zA-Z0-9!&%#?,'=:~]+)\[/link]","<a href=""http://$1"">$1</a>")
    arrPatterns(15) = Array("\[maillink=([@\-_.a-zA-Z0-9]+)]([-_./a-zA-Z0-9 !&%#?,'=:~]+)\[/maillink]","<a href=""mailto:$1"">$2</a>")
    
    
    
    
    
    strInput = "Here's some bold text and " & _
    			"a bold [link=www.mydomain.com]link[/link]. " & _
    			"Here's another [link=http://www.mydomain.com]link[/link] " & _
    			"and heres an [maillink=joe@mydomain.com]email link[/maillink]. " & _
    			"And some [i]Italic text without a closing block."
    
    
    
    strParsedText = PatternReplace(strInput, arrPatterns)
    strHTMLOutput = HTMLEncode(strParsedText) 
    			
    document.write "<b>Original text:</b><br>" & strInput & "<br /><br />"
    document.write "<b>Parsed text:</b><br>" & strParsedText & "<br /><br />"
    document.write "<b>HTML output:</b><br>" & strHTMLOutput & "<br /><br />"
    
    
    
    </script>
    Last edited by shane; Nov 23, 2001 at 08:44.

  13. #13
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Newcastle, England
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad it worked Umair .

    I can't pretend to be a regexp expert by a loooooong shot - I actually read about this exact technique in a tutorial somewhere. I can't for the life of me remember where I read it I'm afraid, but it was fairly brief - just gave the reg exp and explained how you could store the tags you wish to use in a text file/database and use a loop to process them all; which you obviously know how to do .

    Anyway, glad to help a tiny bit. As for tutorials, there's a great chapter in a PERL book from O'Reilly called something like Programming PERL 3rd Edition - it's something like 70 pages on regexp's. Obviously it's slanted for PERL, but apparently it's a good general guide too (I haven't read it yet so can't comment from personal experience, but it's been recommended to me twice now - really should do something about it ). As for ASP-specific tutorials, I haven't found many yet - if you stumble across anything useful please share the URL!
    Nick Wilson [ - email - ]


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •