Created
March 25, 2014 00:32
-
-
Save bennadel/9752534 to your computer and use it in GitHub Desktop.
Ask Ben: Adding A Query String Pair Value To Existing HTML Using ColdFusion (Flagrant Badassery Version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(<a\s[^>]*?href\s*=\s*""[^?#""]*)\??(?![^#""]*?\bsource=) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Alter URL Demo</title> | |
</head> | |
<body> | |
<p> | |
Hey man, if you are looking for some good images, you should probably try out the search page on <a href="http://www.searchgalleries.com?source=bennadel.com" target="_blank">Search Galleries</a>. It's pretty darn comprehensive and seems to keep track of all the free galleries that you will ever need. If you want to mess with the URL, its easy; just add a "q" query string value to the search url. The general site search URL is <a href="http://www.searchgalleries.com/search/" target="_blank">http://www.searchgalleries.com/search/</a>. So, then, to add a query value to it, such as "mature", you would simply add the query string "q=mature" to the url: <a href="http://www.searchgalleries.com/search/?q=mature#links" target="_blank">http://www.searchgalleries.com/search/?q=mature</a>. You can even search for more than one value at a given time. So, for instance, if you want to search for mature brunette women, you would put go to the URL: | |
<a href="http://www.searchgalleries.com/search/?q=mature+brunette#links" target="_blank">http://www.searchgalleries.com/search/?q=mature+brunette</a>. Notice that "mature" and "brunette" are separated by a "+" sign. This is the URL encoded form of a space. | |
</p> | |
</body> | |
</html> | |
<!--- Get the page context. ---> | |
<cfset objPageContext = GetPageContext() /> | |
<!--- Get the page buffer. ---> | |
<cfset objBuffer = objPageContext.GetOut().GetBuffer() /> | |
<!--- | |
Get the content buffer string. This will give us everything | |
that has NOT yet been flushed to the browser. This is just | |
how I am doing it for this demo and is NOT the only way to | |
perform this task. Since this page is small, (and is being | |
tested), we can safely assume that the content has not yet | |
been flushed to the client. | |
---> | |
<cfset strContent = objBuffer.ToString() /> | |
<!--- | |
Steve of Flagrand Badassery has taken my challenge to modify | |
the regular expression in order to handle this replace in | |
one swoop rather than using the Java Pattern / Matcher. Here | |
is my attempt to break down his regular expression: | |
(?i) | |
-- Case insensitive (I added this to use the Java regex | |
-- replace rather than REReplaceNoCase()). | |
-- First Group: | |
( | |
<a\s[^>]*? | |
-- Only match the Anchor tag followed by a space | |
-- followed by a lazy match of non-">" characters. | |
-- The lazy nature of this regular expression will | |
-- try to match the next token (href) when possible. | |
href\s*=\s*""[^?##""]* | |
-- The href attribute with possible spaces around | |
-- the equals sign (nice call! I always forget | |
-- that). Then, quotes followed by any characters | |
-- not include ?, #, or ". | |
) | |
\?? | |
-- Matches the literal "?" zero or one times (an | |
-- optional characters in the URL. | |
-- Negative look ahead: | |
(?! | |
[^##""]*?\b | |
-- A lazy match for any character not including | |
-- # and " followed by a word boundry. | |
source= | |
-- The URL param that we DONT want to add if it | |
-- already exists (hence the negative look ahead | |
-- that we are currently in). | |
) | |
---> | |
<cfset strContent = strContent.ReplaceAll( | |
"(?i)(<a\s[^>]*?href\s*=\s*""[^?##""]*)\??(?![^##""]*?\bsource=)", | |
"$1?source=bennadel.com&" | |
) /> | |
<!--- Clear the existing content buffer. ---> | |
<cfset objPageContext.GetOut().ClearBuffer() /> | |
<!--- Output the updated HTML. ---> | |
<cfset WriteOutput( strContent ) /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Alter URL Demo</title> | |
</head> | |
<body> | |
<p> | |
Hey man, if you are looking for some good images, you should probably try out the search page on <a href="http://www.searchgalleries.com?source=bennadel.com" target="_blank">Search Galleries</a>. It's pretty darn comprehensive and seems to keep track of all the free galleries that you will ever need. If you want to mess with the URL, its easy; just add a "q" query string value to the search url. The general site search URL is <a href="http://www.searchgalleries.com/search/?source=bennadel.com&" target="_blank">http://www.searchgalleries.com/search/</a>. So, then, to add a query value to it, such as "mature", you would simply add the query string "q=mature" to the url: <a href="http://www.searchgalleries.com/search/?source=bennadel.com&q=mature#links" target="_blank">http://www.searchgalleries.com/search/?q=mature</a>. You can even search for more than one value at a given time. So, for instance, if you want to search for mature brunette women, you would put go to the URL: | |
<a href="http://www.searchgalleries.com/search/?source=bennadel.com&q=mature+brunette#links" target="_blank">http://www.searchgalleries.com/search/?q=mature+brunette</a>. Notice that "mature" and "brunette" are separated by a "+" sign. This is the URL encoded form of a space. | |
</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment