Detecting mixed case strings in ASP Classic

I needed to detect a mixed case string in classic ASP. I define a mixed case string as containing both upper and lower case characters, like AuxagGsrLpa. I could not find a free function on the web to make this decision, so I wrote one.

<%
	Function isMixedCase( str )
		'detects mixed case strings using the english alphabet
		'ignores spaces and other non-alphabetic characters
		str = trim( str )
		isMixed = false
		lastCase = ""
		for i=1 to len( str )
			currentChar = mid( str, i, 1 )
			if asc( currentChar ) >= 65 and asc( currentChar ) <= 90 then
				if lastCase <> "" and lastCase <> "upper" then
					isMixed = true
					exit for
				else
					lastCase = "upper"
				end if
			else
				if asc( currentChar ) >= 97 and asc( currentChar ) <= 122 then
					'lower
					if lastCase <> "" and lastCase <> "lower" then
						isMixed = true
						exit for
					else
						lastCase = "lower"
					end if
				else
					lastCase = ""
				end if
			end if
		next
		if isMixed then
			isMixedCase = true
		else
			isMixedCase = false
		end if
	End Function
%>

Here are a few test strings to demonstrate the functionality:


<%
response.write "Hello " & isMixedCase( "Hello " ) & " <br>"
response.write "hello! " & isMixedCase( "hello!" ) & " <br>"
response.write "HELLO " & isMixedCase( "HELLO " ) & " <br>"
response.write "hello there " & isMixedCase( "hello there" ) & " <br>"
response.write "hellothere " & isMixedCase( "hellothere" ) & " <br>"
response.write "hellotherE " & isMixedCase( "hellotherE" ) & " <br>"
%>

Here is the resulting HTML output of the tests:

Hello True
hello! False
HELLO False
hello there False
hellothere False
hellotherE True

Matching MD5 hashes in ASP.NET and ASP Classic

The goal of this page is to put two MD5 code samples on the same web page, one for ASP classic and one for ASP.NET. String formatting (like ASCII vs UTF-8) can trip up coding these two routines. These two code samples will produce the same MD5 hash output.

Classic ASP/VBScript MD5

The ASP code is simple once you have a copy of md5.asp from http://frez.co.uk. The only file in the ZIP that you need is md5.asp.


<!--#include file="md5.asp"-->
<%

response.write md5("email@example.com")

%>

ASP.NET MD5

The keys to getting the .net output to match are encoding.ascii and toString("x2").

<%@ Import Namespace="System.Security.Cryptography" %>
<script language="VB" runat="server">

	Public Sub Page_Load( sender As Object, e As EventArgs )

		Dim strPlainText as String = "email@example.com"

		dim md5Hasher as MD5 = MD5.create( )

		dim result as byte( ) = md5Hasher.computeHash( encoding.ascii.getBytes( strPlainText))

		for each singleByte as byte in result
			response.write ( singleByte.ToString("x2") )
		next

	End Sub

</script>

How to: add images to reddit sidebar

Here is one quick and dirty way to add an image to the sidebar of a subreddit. You must be a moderator of a subreddit in order to edit its stylesheet.

  1. Upload the image
  2. Add this CSS to the stylesheet:
    /* add image to the bottom of the sidebar */
    .side .usertext-body{ padding-bottom: 400px; background: url(%%image_name%%) no-repeat; background-position: bottom center; }
  3. Replace %%image_name%% with the name of your uploaded image
  4. Change padding-bottom: 400px; to a value in pixels that is at least the height of your image

Similarly, here is a CSS code that will add an image to the top of the reddit sidebar:

/* add image to the top of the sidebar */
.side { padding-top: 400px; background: url(%%image_name%%) no-repeat center; }

This adds a background image to the sidebar and positions it at the bottom. The padding creates enough blank space to make the image visible.

This won’t work if you already have a background image on the the sidebar as part of other visual changes on the subreddit.

gif2frames.exe: Extract frames from an animated GIF

I built a Windows 32 bit command line executable, gif2frames.exe. (Go to download)

This tool will take an animated GIF image file and save a separate static image for each frame in PNG, BMP or JPG format.

The reason GIFs are useful is because the files can be optimized to reduce the size on disk. If a frame is visible for 1 or 10 seconds, a single copy of the frame may be stored inside the image file and a frame delay can help the player coordinate the animation.

For this reason, when you say “extract frames from a gif file” you could mean two things; each unique frame could be extracted just once, or each frame could be extracted for each unit of time it is visible. If you want the latter, you must then decide on a rate of frames per second.

Also interesting: the timing is funny business. Minimum frame rates (per second) can be determined by the software displaying the GIF file (more info at the bottom of this page). This makes the number of frames in a film strip style export of frames hard to calculate. I workaround this problem by finding the frame with the shortest display time and divide all the other times by this number. This means animations where all frames are shown for three seconds will only produce one image for each frame. If this is not what you are looking for, try ImageMagick (convert.exe).

Usage

Here are a few cmd.exe examples showing basic calls.

Animated GIF to PNG images

C:\>gif2frames.exe at.gif

Animated GIF to BMP images

gif2frames at.gif -bmp

Animated GIF to JPG images

gif2frames at.gif -jpg

Only unique frames

gif2frames at.gif -png -unique

Downloads

Top users WordPress plugin

I wrote a new plugin to list the contributors to a WordPress website. The comment and post counts are added together, and the top X users are listed in a simple table alongside the counts.

See this plugin in use at http://www.fightthescams.com/thanks-for-commenting/

Download top-users-by-comment-plus-post-count.zip.

Installation instructions

  1. Upload the `top-users-by-comment-plus-post-count` folder to the `/wp-content/plugins/` directory
  2. Activate the plugin through the ‘Plugins’ menu in WordPress
  3. Place this or a similar function call in your theme: <?php top_users_by_comment_plus_post_count( 10 ); ?>

CSS sample

This is the CSS I am using to style the output.

#top-users-by-comment-plus-post-count td.tu-author{ padding-right: 8px; }
#top-users-by-comment-plus-post-count td.tu-count{ text-align: right; }

Sample output

I added tab and line break characters for readability.

<table id="top-users-by-comment-plus-post-count">
	<tr><td class="tu-author">DisgustedInDenver</td><td class="tu-count">297</td></tr>
	<tr><td class="tu-author">Corey</td><td class="tu-count">109</td></tr>
	<tr><td class="tu-author">G11</td><td class="tu-count">60</td></tr>
	<tr><td class="tu-author">jmbecker13</td><td class="tu-count">10</td></tr>
	<tr><td class="tu-author">trustmedotcom</td><td class="tu-count">9</td></tr>
	<tr><td class="tu-author">ulfwolf</td><td class="tu-count">8</td></tr>
	<tr><td class="tu-author">noscamsforme</td><td class="tu-count">8</td></tr>
	<tr><td class="tu-author">who8dapple</td><td class="tu-count">7</td></tr>
	<tr><td class="tu-author">cindmo</td><td class="tu-count">7</td></tr>
	<tr><td class="tu-author">nytemare4u</td><td class="tu-count">7</td></tr>
</table>

Disable comment author links in WordPress

I just wrote a new plugin to disable commenter names from linking to the website URLs they may provide when commenting.

Some themes allow commenters to provide a home page URL along with their comment. The comment author’s name then becomes a link to that website wherever it appears on your site. This plugin removes those links.

Download disable-comment-author-links.zip

Installation instructions

  1. Upload the `disable-comment-author-links` folder to the `/wp-content/plugins/` directory
  2. Activate the plugin through the ‘Plugins’ menu in WordPress

WP: Add content to the bottom of every post

I wrote a new WordPress plugin last week, July of 2011. This plugin is a simple filter that adds some content to the bottom of each post. I thought for sure a plugin like this would already exist, and I was surprised to find a few very complex solutions with way more features than necessary.

This plugin has no other features and adds no administration options pages in your admin dashboard (because you can edit the settings via the Plugin editor). The message that is appended to each post is saved in a text file in the plugin directory. I chose a file on disk instead of a WordPress database option because I dislike plugin options pages for simple plugins.

How to edit the message

To edit the message that will be added to the bottom or footer of your posts, Go to Plugins > Editor and choose “Bottom of every post” with the top right selector. The plugin’s files will be loaded on the right. Choose the file `bottom_of_every_post.txt` and edit at will.

Download bottom-of-every-post.zip

Installation instructions

  1. Modify `bottom-of-every-post.txt` to contain the content you would like at the bottom of every post
  2. Upload the `bottom-of-every-post` folder to the `/wp-content/plugins/` directory
  3. Activate the plugin through the ‘Plugins’ menu in WordPress

I am also going to use this code to teach a few people how to create their own WP plugins. Here is the full source code of my new plugin:


<?php
/*
Plugin Name: Bottom of every post
Plugin URI: http://www.tacticaltechnique.com/wordpress/bottom-of-every-post/
Description: Add some content to the bottom of each post.
Version: 1.0
Author: Corey Salzano
Author URI: http://profiles.wordpress.org/users/salzano/
License: GPL2
*/

/*
	avoid a name collision, make sure this function is not
	already defined */

if( !function_exists("bottom_of_every_post")){
	function bottom_of_every_post($content){

	/*	there is a text file in the same directory as this script */

		$fileName = dirname(__FILE__) ."/bottom_of_every_post.txt";

	/*	we want to change `the_content` of posts, not pages
		and the text file must exist for this to work */

		if( !is_page( ) && file_exists( $fileName )){

		/*	open the text file and read its contents */

			$theFile = fopen( $fileName, "r");
			$msg = fread( $theFile, filesize( $fileName ));
			fclose( $theFile );

		/*	append the text file contents to the end of `the_content` */
			return $content . stripslashes( $msg );
		} else{

		/*	if `the_content` belongs to a page or our file is missing
			the result of this filter is no change to `the_content` */

			return $content;
		}
	}

	/*	add our filter function to the hook */

	add_filter('the_content', 'bottom_of_every_post');
}

?>

“Time ago” formatting in ASP classic

Today, I needed to convert a time stamp like “1/25/2011 10:42:11 AM” to a readable sentence format like “2 months and 6 hours ago.” Here’s the code I came up with:


	function timeAgo( byval time )
		sentence = ""
		hits = 0
		piecesAgo = array( 0, 0, 0, 0, 0, 0 )
		piecesTotals = array( 0, 12, 30, 24, 60, 60 )
		labels = array( "year", "month", "day", "hour", "minute", "second" )
		dateAddSymbols = array( "yyyy", "m", "d", "h", "n", "s" )

		for p=0 to uBound( piecesAgo )
			piecesAgo( p ) = 0 + datediff( dateAddSymbols( p ), time, now( ))

			if piecesAgo( p ) <> 0 then
				time = dateAdd( dateAddSymbols( p ), piecesAgo( p ), time )
			end if
		next

		'remove negative values
		for p=uBound( piecesAgo ) to 0 step -1
			if piecesAgo( p ) < 0 and p > 0 then
				piecesAgo( p ) = piecesAgo( p ) + piecesTotals( p )
				piecesAgo( p-1 ) = piecesAgo( p-1 ) - 1
			end if
		next

		for p=0 to uBound( piecesAgo )
			if piecesAgo( p ) <> 0 then

				if len( sentence ) > 0 then
					sentence = sentence & " and "
				end if
				sentence = sentence & piecesAgo( p ) & " " & labels( p )
				if piecesAgo( p ) > 1 then
					sentence = sentence & "s"
				end if
				hits = hits + 1
				if hits >= 2 then
					exit for
				end if
			end if
		next

		set piecesAgo = nothing
		set piecesTotals = nothing
		set labels = nothing
		set dateAddSymbols = nothing

		timeAgo = sentence & " ago"
	end function

Export comments WordPress plugin

I launched a new WordPress plugin to export a blog’s comments. Specifically, this plugin creates a tab-delimited text version of the wp_comments database table. Options include approved, pending or spam (or any combination of) comments.

This page will soon expand. For now…

Download this plugin from WordPress.org

WordPress comment paging creates duplicate content

I learned something today about WordPress that I would like to share. The “break comments into pages” feature can create duplicate content for all posts that have only a single page of comments. I made this graphic to explain exactly what happens. Click to enlarge:

Break comments into pages at your own risk

Next Page »

 

Thanks for reading!

Sign up for email updates: