Home

EasyTabFinder

Enter a song below to find the best rated chords tablature for that song.


Song name:

Top

Concept

Looking for the tablature on the internet can be time-consuming, for the simple reason that there are so many sources and different versions. A search for Brothers In Arms on Ultimate-Guitar.com, for example, returns 16 different tabs. This script performs a search for the specified song and returns the simplest to play, highest rated and most rated tab.

Top

How It Works

The search is performed on the web server by a PHP script. The script uses the search function on Ultimate-Guitar.com and filters the tabs by chord tabs, then returns the highest rated tab with the most amount of ratings. Most of the work is done by regular expressions.

Top

The Code

Put a simple form in your html file:

<form action="findTab.php" method="post">
	Song name: <input type="text" name="song" id="song" />
	<br /><br />
	<input type="submit" value="Find" />
</form>
	

The form calls a file called findTab.php that gets and echoes the html of the tab:

<?php

// get the submitted song name
$song = $_POST['song'];

// replace white spaces with plus signs
$song = str_replace(" ", "+", $song);

// set the site and the path that we will use to get the search results (specific to the site used)
$site = "http://www.ultimate-guitar.com";
$path = $site."/search.php?s=".$song."&w=songs";

// we use the php file function to get the search results and put them into a single string
$html = implode("", file($path));

// check if there is a link to a chord tab in the search results
if (strpos($html, "crd.htm") === false) {
	echo "Song not found";
}
		
else 
{
	// use a regular expression to get the filenames of all of the chord tabs in the results
	$link_pattern = '/"(.*)crd.htm"/';
	preg_match_all($link_pattern, $html, $link_matches);
	$links = $link_matches[0];
	
	// use a regular expression to get the rating of each chord tab
	$rating_pattern = '/crd.htm"(.*?)\\n(.*?)_img\/r(.*?).gif/';
	preg_match_all($rating_pattern, $html, $rating_matches);
	$ratings = $rating_matches[3];
	
	// use a regular expression to get the number of ratings for each chord tab
	$num_ratings_pattern = '/crd.htm(.*?)\\n(.*?)<font class="upc">(.*?)<\/font>/';
	preg_match_all($num_ratings_pattern, $html, $num_ratings_matches);
	$num_ratings = $num_ratings_matches[3];
	
	// get the name of the chord tabs with the highest rating - if there are more than one then select the one with the highest number of ratings
	$max_index = 0;
	$max_rating = 0;
	$max_num_ratings = 0;
				
	for ($i=0; $i < count($ratings); $i++) {
		if ($ratings[$i] > $max_rating || ($ratings[$i] == $max_rating && $num_ratings[$i] > $max_num_ratings)) {
			$max_index = $i;
			$max_rating = $ratings[$i];
			$max_num_ratings = $num_ratings[$i];
		}
	}			
	
	// get the link and remove the quotes on either side of it
	$link = $links[$max_index];
	$link = substr($link, 1, strlen($link) - 2);
	
	// set the full path of the link that we want to follow
	$path = $site.$link;
	
	// get the contents of the path
	$html = implode("", file($path));		
	
	// use a regular expression to get the link to the printable version of the tab
	$print_pattern = '/"\/print.php\?what=tab&id=(.*?)"/';
	preg_match($print_pattern, $html, $print_matches);
	$print_link = $print_matches[0];
	
	// remove the quotes on either side
	$print_link = substr($print_link, 1, strlen($print_link) - 2);
	
	// set the full path of the printable version of the tab
	$print_path = $site.$print_link;
	
	// get the contents of the path
	$html = implode("", file($print_path));
	
	// return the html code
	echo $html;
}

?>
	
Top

AJAX

I put in a bit of AJAX so that a page refresh is not required. I used Prototype and Scriptaculous, two free JavaScript libraries that I recommend you check out if you haven't already. They make most of effects on this site possible. Once you have downloaded the libraries, include them within the head tags in your html file:

<script type="text/javascript" src="/js/prototype.js"></script>
<script type="text/javascript" src="/js/scriptaculous.js"></script>
	

Then put the following code in where the form is:

<form onsubmit="sendSongRequest(); return false;">
	Song name: <input type="text" name="song" id="song" />
	<br /><br />
	<input type="submit" value="Find" />
</form>

<br />
<div id="songindicator" style="display:none;"><img src="http://www.napyfab.com/ajax-indicators/images/indicator_circle_ball.gif" /></div>

<div id="songtab" style="display:none;"></div>
	

There are some nice activity indicators here, but I prefer making my own with this cool activity indicator generator. Finally, add the javascript functions that will be called when the form is submitted:

<script type="text/javascript">
function sendSongRequest() {
	$('songtab').innerHTML = "";
	Element.show('songindicator');
	new Ajax.Request("findTab.php", { method: 'post', postBody: 'song='+$F('song'), onComplete: showSongResponse });
}
function showSongResponse(req){
	Element.hide('songindicator');
	Element.show('songtab');
	$('songtab').innerHTML = req.responseText;
}
</script>
	
Top

Download

You can download the source files here:

Comments

Blank Avatar
Bastian
30 July 2008


Nice function!!
But, if I enter the name of a song, say, "smoke on the water" then I get a php error. Is this fixable? Does ultimate-guitar now uses another search method? I really like to put this function on my website!

A PHP Error was encountered
Severity: Warning

Message: file() [function.file]: php_network_getaddresses: getaddrinfo failed: Name or service not known

Filename: controllers/projects.php

Line Number: 257


Leave A Comment

name (required)
email (required but will be kept completely private)
website