Apr
12
It’s up! Santorum Mad Libs Is Now Live

Santorum Mad Libs

Hey everyone! Here’s a project I’ve been working on for the past two weeks and what better time to let it go live than right after good ol’ Ricky resigns.

Santorum Mad Libs

More about the project after the jump.

Why This?

A little while back, a few of my roommates and I were talking about Rick Santorum and how everything he says is hilarious. My one friend jokes, it’s like he talks in Mad Libs. And there it was, a great idea. I’ve been looking for a small little project like this I could use my database and PHP skills and this was perfect. What better way to prove how ridiculous Santorum is then challenging the people to make his quotes even wilder than they are. In the end, the truth was stranger than fiction.

The Database

The database was simple enough. It is one table with three columns, an id (the primary key), lib (the quote), and the date of the quote. I thought of many different ways to store the quotes, but I came to the conclusion that the best way to store the quotes, with the missing word would be to put it all in one string, encasing the missing word in a delimiter. Here is an example:

"The {noun} {verb-ed} the cat"

The Code

Now onto implementation. I wanted the site to be simple. Right when a user visits the site, they are prompted with input fields for the missing words. They input their relevant words, hit submit, and the funny happens. To do this, I would have to call in a random quote from the database, easy enough. Just write a function that counts the quotes and returns a random id.

// returns count of libs in database
function getNumOfLibs() {
	$sqlstr = mysql_query('SELECT * FROM libs');
	return mysql_numrows($sqlstr);
}
// returns random lib id
function getRandomLib() {
	$num = getNumOfLibs()-1;
	return rand(0,$num);
}

Once we’ve gotten our random quote, we need a way to parse through the quote to pull out the missing words. Within the madLib class, I have a default constructor with an array property containing all the possible parts of speech for the missing words. Then I have three functions that will get the missing words. This first function that I found in the comments of php.net returns an array of the positions of all the occurrences of a given string.

function strpos_recursive($haystack, $needle, $offset = 0, $results = array()) {
	$offset = strpos($haystack, $needle, $offset); // the haystack is the quote we're searching, the needle is the string we're looking for
	if($offset === false) {
		return $results;
	} else {
		$results[] = $offset;
		return $this->strpos_recursive($haystack, $needle, ($offset + 1), $results); // recursively call the function with an incremented offset
	}
}

The next two functions I wrote that get the positions of all the missing words in the word and then a function that uses the position to get the missing word.

// returns an arry of all tags in lib
function getTags() {
	$text = $this->text;
	$positions = $this->getTagPositions($text);
	foreach ($positions as $key=>$value) {
		$i=$value+1;
		$word = '';
		while($text[$i]!='}'){
			$word = $word.$text[$i];
			$i++;
		}
		$this->tagsPos[$key] = $word;
		$this->tags[$key] = $this->defaultTags[$word];
	}
}

// returns array of tag positions
function getTagPositions($t) {
	$delim = '{';
	return $this->strpos_recursive($t,$delim);
}

After the user submits their input, most of the work is done. All the words are sent with a $_GET and we then rebuild the new quote with their input on a display page.

What’s next?

Not sure how much further I’ll take this project. It was really just a quick thing I wanted to whip up, however, it would be nice if I could modify the code to handle the ability to use a user’s input more for a missing word more than once in the quote, making for a more cohesive story. Stay tuned for any updates and if you got some better ideas for the implementation, let me know, I’m always eager to hear alternatives.

View The Source

You can check out the source on the GitHub page.

April 12, 2012 Tags: , , ,

Share on: Facebook | Twitter