SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Regular Expressions & Custom Tags

    Hi peeps,

    I have created a template system, and someone pointed a script out that would let me create simple links by using [link]home|http://www.sitepoint.com[/link] which would convert anything between those custom tags into a link, with home being the text displayed and the web address the location.

    Now I need a way to be able to edit the script, well the RE Pattern so I can use it for other things.

    My script is:

    Code:
    <%
    
    	BaseHREF = "http://localhost/root3/"
    
    	sub welcome_banner()
    	
    		dayname = day(now)
    		monthlyname = monthname(month(now))
    		
    		if dayname = "1" or dayname = "21" or dayname = "31" then
    			
    			dayname = dayname & "<img src='../../images/Template_General/shim.gif' alt='dateprefix' width='2' align='top'><img src='../../images/Template_General/dateprefix-sst.gif' alt='dateprefix' align='top'> "
    		
    		elseif dayname = "2" or dayname = "22" then
    		
    			dayname = dayname & "<img src='../../images/Template_General/shim.gif' alt='dateprefix' width='2' align='top'><img src='../../images/Template_General/dateprefix-nd.gif' alt='dateprefix' align='top'> "
    			
    		elseif dayname = "3" or dayname = "23" then
    		
    			dayname = dayname & "<img src='../../images/Template_General/shim.gif' alt='dateprefix' width='2' align='top'><img src='../../images/Template_General/dateprefix-rd.gif' alt='dateprefix' align='top'> "
    			
    		else
    		
    			dayname = dayname & "<img src='../../images/Template_General/shim.gif' alt='dateprefix' width='2' align='top'><img src='../../images/Template_General/dateprefix-fx.gif' alt='dateprefix' align='top'> "
    			
    		end if
    		
    		if len(monthlyname) > 5 then
    		
    			monthlyname = monthlyname & left(monthlyname, 5)
    			
    		end if		
    		
    		if session("userhash") <> "" then
    		
    			response.write "Welcome User"
    		
    		else
    		
    			response.write(weekdayname(weekday(now)) & " "  & dayname & " " & monthlyname & " " & year(now)) 
    		
    		end if
    		
    	end sub
    	
    	sub pull_template(file)
    	
    		dim grab_template
    		set grab_template = server.createobject("Microsoft.XMLHTTP")
    		if inStr(file, "http://") then
    		
    			grab_template.Open "GET", file, false
    		
    		else
    		
    			grab_template.Open "GET", BaseHREF & file, false
    		
    		end if
    		
    			grab_template.Send
    			
    		template_info = grab_template.responseText
    		
    		if inStr(file, "http://") then
    		
    			template_info = replace(template_info, "images/", file & "/images/")
    			template_info = replace(template_info, "files/", file & "/files/")
    			template_info = replace(template_info, "background-color: #262A32;", "background-color: #FFFFFF;")
    			template_info = replace(template_info, "background-color: #730000;", "background-color: #FFFFFF;")
    		else
    			
    			template_info = replace(template_info, "../../", BaseHREF)
    			template_info = replace(template_info, "", "")
    					
    		end if
    		
    		template_info = ConvertLinks(template_info)
    		template_info = ConvertLinks(template_info)
    		
    		response.write template_info
    		
    		set grab_template = nothing
    		
    	end sub
    	
    Function ConvertLinks(strLINK)
    	Set oRE = New RegExp
    	oRE.IgnoreCase = True
    	oRE.Global = True
    	oRE.Pattern = "\[link\]([^\[]+)\|([^\[]+)\[/link\]"
    	
    	ConvertLinks = oRE.Replace(strLINK,"<a href=""$2"" border=""no"">$1</a>")
    	
    	set ORE = nothing
    
    End Function	
    
    Function ConvertBold(strBOLD)
    	Set oRE = New RegExp
    	oRE.IgnoreCase = True
    	oRE.Global = True
    	oRE.Pattern = "\[b\]([^\[]+)[/b\]"
    	
    	ConvertLinks = oRE.Replace(strBOLD,"<b>$1</b>")
    
    	set ORE = nothing
    
    End Function	
    %>
    as you can see I have tried to edit it, but trying to decrypt the patter is totally stupid, all I want to do is replace blah with <b>blah</b>... I know its a simple thing to do using just HTML ( rather then having to use some coding.

    I want to learn how to change it, as I need to start creating my Control panel for users, which will need alot of this.

    Cheers

    I'm not sure whether this forum e-mails me with a response when you reply, if not could you reply to admin[no-spam]@eycdwebdesign.co.uk

    TIA

    Gav

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just use a stylesheet....takes care of all your links

    <style type="text/css">
    <!--
    A:link{color:black;font-weight: bold;text-decoration:underline}
    A:visited{color:black;font-weight: bold;text-decoration:underline}
    A:hover{background:af0000;color:white;text-decoration:none}
    A:active{color:af0000;text-decoration:none}
    -->
    </style>
    good luck

  3. #3
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    erm... I think you have not seen what I am doing, I am using reg expressions to create links, not format them.

    Gav

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahem....replace() is string formatting
    oRE.IgnoreCase is string formatting
    oRE.Pattern is char formatting
    <b>blah</b> is formatting (changing reg text to bold; hence formatted, not created

    however, i did misread the original tags - i stand corrected

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hope this helps, I fixed up the regexp to be non greedy (there was a bug before).

    Modify the ConvertFromTemplate function to include the tags you want - the way that most of the patterns work is to search for the beginning and end tags and store what's between them (in the case of the <a> tag there are two pieces of data between them separated with a |).

    All the backslashes are there because the characters that come after them are regexp special characters but we want to use them as literals.

    Keep in mind that running the regexp replace will replace ALL of the tags in the string you are passing.

    Code:
    <%
    Function ConvertFromTemplate(sTemplate)
    	sTemp = sTemplate
    	sTemp = ConvertTemplate(sTemp, "\[link\](.+?)\|(.+?)\[/link\]", "<a href=""$2"" border=""no"">$1</a>")
    	sTemp = ConvertTemplate(sTemp, "\[b\](.+?)\[/b\]", "<b>$1</b>")
    	sTemp = ConvertTemplate(sTemp, "\[i\](.+?)\[/i\]", "<i>$1</i>")
    	sTemp = ConvertTemplate(sTemp, "\[email\](.+?)\[/email\]", "<a href=""mailto:$1"">$1</a>")
    	
    	ConvertFromTemplate = sTemp
    End Function
    
    Function ConvertTemplate(sPage, sPattern, sReplace)
    	Set oRE = New RegExp
    	oRE.IgnoreCase = True
    	oRE.Global = True
    	oRE.Pattern = sPattern
    	
    	ConvertTemplate = oRE.Replace(sPage, sReplace)
    	
    	set ORE = nothing
    
    End Function
    
    sPage = "This is my web page template<br>" & _
    	"This line has a link [link]to Sitepoint|http://www.sitepoint.com[/link]<br>" & _
    	"This line has some [b ]bold[/b ] text<br>" & _
    	"This line has a link [link]to Google|http://www.google.com[/link]<br>" & _
    	"This line has some [b ][i ]italic bold[/i ][/b ] text<br>" & _
    	"This line has an email address someguy@somewhere.com<br>"
    	
    Response.Write ConvertFromTemplate(sPage)
    %>
    (remove the spaces that are in some of the tags so that the forum doesn't convert them)

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well thank you all for the help. I should of updated the post, as I have found a great site that provides patterns for almost anything.

    So what ever I am doing, I just need to insert this pattern and it works )

    maybes you would like it:

    http://www.regxlib.com/default.aspx

    Gav


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
  •