The first code snippet is a little function that converts text into mixed case, or sentance case. It basically seeks out spaces in a string and converts the first character after thhe space to a capitalized letter and the remaining characters before the next space to lower case.
ASP seems to lack a URL Decode function but has a URL Encode function available. Here is a function that can decode any URL Encoded URL or variable.
Here is a practical example of when you would use the URLDecode function - to record the search terms someone has use to find your site in Google. This functions examines the string to see if it contains "google." and if it does, assumes the string contains google keywords and seeks out "q=" inside the string. If it also finds this, the stripStr is now created with the prefix "GOOGLE:" and the key words are appended to the end.
This next function is a short and sweet function that will strip HTML tags out of any string. It uses regex expressions and is very fast.
This snippet is a little function that strips out specified special characters. It basically seeks out any of the characters that exist in the array and removes them all.
This next snippet is a simple function that will check an array for any occurances of a particular string.
Lately I have needed to perform a lot of redirects from old pages to new pages. Some of these old pages have decent rankings in search engines, so how do I make sure these new pages retain this ranking? By using a HTTP 301 Redirect (HTTP Moved Permanently). Here is how you code a 301 redirect in ASP:
If you are wanting to redirect to your default page name (index.asp,default.asp etc.), it is a good idea to redirect only to the folder name and leave off the name of the page like:
Response.AddHeader "Location", "/subfolder/"
There are many times were you have wanted to grab something from another site that isn't provided via RSS. You could type it in, but that would be time consuming. So how do we do this? We scrap the content from the site using the XMLHTTP object. If you have read the previous article on how to cache an RSS feed, you will no doubt say many similarities here in this article, so I won't re-explain those portions. So let's have a look at the code.
The code contains two functions, LoadThePage and GrabTheContent, and there names self explain what they perform. LoadThePage saves a copy the external HTML into an XMLHTTP object. GrabTheContent manipulates this object and returns a string that we can use. We give this function two paramters, the text at the start of the section we want to grab and the text that ends what we want to grab. Pretty simple. For this example, we will be using http://birdsinbackyards.net/species/Cacatua-roseicapilla as the page we retrieve text from.
Now the main portion of the program firstly it checks the date stamp of the application variable from when it was last cached and if necessary, retrieves a new cache. We do this so we don't hammer someone else's website and slow down the performance of our page (and to not annoy the other webserver with heaps of connections). Once we have either retrieved a new cache or used the existing one, we display the contents on the page. So we would see these results:
The Galah can be easily identified by its rose-pink head, neck and underparts,
with paler pink crown, and grey back, wings and undertail. Birds from the west of
Australia have comparatively paler plumage. Galahs have a bouncing acrobatic flight,
but spend much of the day sheltering from heat in the foliage of trees and shrubs.
Huge noisy flocks of birds congregate and roost together at night.
Ok. So that is nice but how would we format this retrieved text? How do you remove the HTML tags?
To strip the HTML tags, we will be using a function I have previously published named stripHTML. What this function does is strip ALL HTML tags using regex and returns plain text. Now to format the text appearance, we will need to use the string REPLACE function to change/remove/insert tags into appropriate places. Essentially, you scan through the retrieved page and add in the formatting you like. We will finally present this text inside a DIV with an inline style applied. So we would firstly add in the stripHTML function as shown previously on this page and then finally, we would change how the text is presented at the bottom of the script to this:
So let's take a look at how it appears now:
The Galah can be easily identified by its rose-pink head, neck and underparts, with paler pink crown, and grey back, wings and undertail. Birds from the west of Australia have comparatively paler plumage. Galahs have a bouncing acrobatic flight, but spend much of the day sheltering from heat in the foliage of trees and shrubs. Huge noisy flocks of birds congregate and roost together at night.
There you go, much better - and exactly the same results as the first example but much more readable and adaptable to your site after using your formatting. You could now proceed a step further and change this to use classes rather than using inline styles.
If like me you work a lot with XML and RSS, it will be necessary to cache it to reduce the load on systems. ASP would normally request the file each and every time the script is loaded in the browser. The following method caches an RSS feed for a specified period of time, thus reducing the load on a server greatly. I am going to assume you understand the RSS 2.0 format in this example.
Essentially, the script checks the timestamp of the application variable, and if it has been longer than 1 hour, it will reload the RSS feed into an application variable, otherwise it will use the existing content inside the application variable rss-html. If the script needs to retrieve the RSS feed, it loads it into a DOM object and then parses the RSS looking for <item> tags. We then create a string for each of the RSS fields we are interested in storing. While we parse each <item> we add a delimiter inside these strings "#%#" to separate the entries. Once we have finished parsing the RSS, we are left with three strings.
We now split these strings into arrays using the split function, seeking the delimiter "#%#" we previously used. Once the elements are inside three arrays, we loop through with a FOR loop until we reach the end of the arrays. We can process each of the elements as we loop through and format as required. The reason I prefer not to feed the RSS straight into an array is because we would need to determine the number of records and redim the array, where as with the method I have used, you create a concatenated string that can be easily split later at the delimiters and feed into an array.