Location: PHPKode > scripts > PHP Movie Listings > php-movie-listings/phpMovieListings.php
<?php
#######################################################
#													  
#                   phpMovieListings 				  
#													  
#   Write By: Dave - AdvancedStyle.com                
#   Description: 									  
#    This class uses the IMDB movie database to       
#    generate real-time movie listings.  Because      
#    it relies on the IMDB site if their site is down 
#    this class will not product a movie list, and if 
#    IMDB decides to recode their site this script    
#    will also need to be adjusted.                   
#                                                     
#    This class include no warranty, or guarantee.    
#    If you have questions, comments or find bugs    
#    you can contact me at hide@address.com     
#    Any emails written in a demanding tone will be   
#    ignored.                                         
#													  
#######################################################


class phpMovieListings{

	var $default_zip;
	var $listings_content;
	var $imagefolder;
	
	function phpMovieListings($zipcode, $image_folder=''){
			
			// Check to see if the user has entered a different zip code
			$this->default_zip = $zipcode;
			$this->imagefolder = $image_folder;
			if(isset($_GET['zip']) && strlen($_GET['zip']) > 4){
				// If user has entered a valid zip then use it
				$zip = $_GET['zip'];
				$view .= '&zip='.$_GET['zip'];
			}else{
				// If not then use the default zip code (specified when the class is initalized)
				$zip = $this->default_zip;
			}
			// Check if the user has change the distance area
			if(isset($_GET['distance']) && strlen($_GET['distance']) > 2 && strstr($_GET['distance'],"m")){
				$view .= '&distance='.$_GET['distance'];
					$extras = '/'.$_GET['distance'];
			}
			// Check if the user has changed the listing style
			if($_GET['type'] == 'movielist'){
					$extras .= '/movie';
					$viewby = 'movie';
			}
			// Check if they've changed the date
			if(isset($_GET['date'])){
					$extras .= '/'.$_GET['date'];
					$view .= '&date='.$_GET['date'];
			}
			// Start creating the toplinks
			if($viewby == 'movie'){
				$viewlink = '<a href="'.$_SERVER['PHP_SELF'].'?type=theatre'.$view.'">Theater</a> | <b>Movie</b>';
			}else{
				$viewlink = '<b>Theater</b> | <a href="'.$_SERVER['PHP_SELF'].'?type=movielist'.$view.'">Movie</a>';
			}
			$date_list = '<select name="date" onChange="javascript:this.form.submit();">';
				for($i=0;$i<7;$i++){
					$day = strtotime("+".$i." days");
					$selected = '';
					if($_GET['date'] == date("Y-m-d",$day)){
						$selected = ' SELECTED';
					}
					if($i > 0){
						$display_date = date("l, F jS",$day);
					}else{
						$display_date = 'Today, '.date("F jS",$day);
					}
					$date_list .= '<option value="'.date("Y-m-d",$day).'"'.$selected.'>'.$display_date.'</option>';
				}
			$date_list .= '</select>';
			
			$distances = '<select name="distance" onChange="javascript:this.form.submit();">';
				for($i=10;$i<=50;$i+=5){
					$selected = '';
					if($i == $_GET['distance']){
						$selected = ' SELECTED';
					}
					$distances .= '<option value="'.$i.'m"'.$selected.'>'.$i.' Miles</option>';
				}
			$distances .= '</select>';
			$top_links = '<form action="'.$_SERVER['PHP_SELF'].'" method="GET"><table cellspacing="0" cellpadding="3" width="100%" style="border:1px solid #333333;"><tr><td class="heading">View by:</td><td class="heading">Date:</td><td class="heading" colspan="2">Zipcode:</td><td class="heading">Distance:</td></tr>';
			$top_links .= '<tr><td class="main">'.$viewlink.'</td><td class="main">'.$date_list.'</td><td class="main"><input type="text" name="zip" value="'.$zip.'"></td><td><input type="submit" value="Go"><td class="main">'.$distances.'</td></tr></table></form><hr>';
			// Top Links and forms are completed
			
			// Get the data fromt he IMDB site!
			$url = "http://imdb.com/showtimes/location/".$zip.$extras;
				$handle = fopen($url, "rb");
				$contents = '';
				while (!feof($handle)) {
				  $contents .= fread($handle, 8192);
				}
				fclose($handle);
		// Start stripping out the unwanted links/images and formatting
		$contents = strstr($contents,'<table class="showtimes tabular"');
		$contents = $this->stristr_reverse($contents,'<p class="showtimes">');
		$contents = $this->replaceAHREF($contents);
		$replace[] = 'http://i.imdb.com/images/showtimes/';
		$with[] = $this->imagefolder;
		$replace[] = '<img src="'.$this->imagefolder.'fandango_bbbbff.gif" width="15" height="15" alt="Fandango">';
		$with[] = '';
		$replace[] = '<img src="'.$this->imagefolder.'fandango.gif" width="15" height="15" alt="Fandango">';
		$with[] = '';
		$replace[] = 'bgcolor="#bbbbff"';
		$with[] = '';
		$replace[] = '(add to My Theaters)';
		$with[] = '&nbsp;';
		$replace[] = 'showtimes tabular';
		$with[] = 'showtimes';
		$replace[] = 'Buy Tickets';
		$with[] = 'Showtimes';
	
		$contents = str_replace($replace,$with,$contents);
		$replace = array();
		$with = array();
		
		$contents = $this->stripHTML($contents,'(',')');
			for($i=0;$i<=10;$i=($i+.1)){
			$replace[] = '<small><b>'.number_format($i,1).'</b>/10 </small>';
			$with[] = '&nbsp;';
		}
		
		$contents = str_replace($replace,$with,$contents);
		// Return the listing and top links/form
		$this->listings_content = $top_links.$contents;
	
	}
	
	// Show the listings
	function showListings(){
		return $this->listings_content;
	}
	// This fucntion i wrote to strip out the HTML (It's messy, but it works, if you think you can do better, feel free to rewrite it with a preg_replace)
	function stripHTML($string,$first_symbol = '<', $second_symbol = '>'){
		$count = 0;
		$temp_string = $string;
		while(strstr($temp_string,$first_symbol)){
			$pos = strpos($temp_string,$first_symbol);
			$temp_string = substr($temp_string,($pos+1),strlen($temp_string));
			$count++;
		}
		
		$temp = $string;
		for($i=0;$i<$count;$i++){
			$pos = strpos($temp,$first_symbol);
			$temp_before = substr($temp,0,$pos);
			$temp = substr($temp,$pos,strlen($temp));
			$pos = strpos($temp,$second_symbol);
			$remove_array[] = substr($temp,0,($pos+1));
			$with_array[] = "";
			$temp= substr($temp,($pos+1),strlen($temp));
		}
		$string = str_replace($remove_array,$with_array,$string);
		
		return $string;
	}
	
	// Another messy function, this time to replace the links
	function replaceAHREF($string){
		$string = str_replace("</a>","",$string);
		$count = 0;
		$temp_string = $string;
		while(strstr($temp_string,"<a")){
			$pos = strpos($temp_string,"<a");
			$temp_string = substr($temp_string,($pos+2),strlen($temp_string));
			$count++;
		}
		
		$temp = $string;
		for($i=0;$i<$count;$i++){
			$pos = strpos($temp,"<a");
			$temp_before = substr($temp,0,$pos);
			$temp = substr($temp,$pos,strlen($temp));
			$pos = strpos($temp,">");
			$remove_array[] = substr($temp,0,($pos+1));
			$with_array[] = "";
			$temp= substr($temp,($pos+1),strlen($temp));
		}
		$string = str_replace($remove_array,$with_array,$string);
		
		return $string;
	}
	
	// Function to do a reverse stristr (can be found on the PHP.net site)
	function stristr_reverse($haystack, $needle) {
	  if(strpos($haystack, $needle)){
		$pos = strpos($haystack, $needle);
		return substr($haystack, 0, $pos);
	  }else{
		return $haystack;
	  }
	} 
	
}
?>
Return current item: PHP Movie Listings