Skip to main content
Snippets

April Fools Joke Using File Get Contents

By March 31, 2016No Comments

A few days ago I wrote a post about readfile(); and file_get_contents();. For April Fools day I decided to put it into action using file_get_contents(); to effectively take a local news article and change whatever I wanted.

Here are the technical details, but you are welcome to jump right to the code below.

I was struggling with creating an array of keys and values to check against the page contents then find / replace. I naturally hit stack exchange and asked for help. As always, a kind user was well informed and set me in the right direction. Thanks jDo.

Here’s the question on Stack Exchange.

Essentially what’s happening is the original page is fetched and then pre hypertext processed (php), which is the code below. This happens on the server before it gets to your browser window, known as “server side scripting.” Once the page is rendered in your browser, the original page then fires off any hypertext markup language (html), cascading sylesheets (css), and fetches any javascript (js), which is called client side scripting.

If you enjoy this, consider sharing my public post on Facebook.

My April Fools prank page – the code

*you can follow each step by reading the comments highlighted in light blue and pre-pended with //
[su_divider]

//
// Let's April Fool Using file_get_contents();!	
//
	//
	// Define the file we want to get contents from in a variable
	//
	$readfile = 'http://www.lex18.com/story/27851612/lexington-police-searching-for-bank-robbery-suspect';
	//
	// Variable to read the contents of our file into a string
	//
	$pull = file_get_contents($readfile);
	//
	// Define image URLs to be replaced
	//
	$image_1 = 'http://WLEX.images.worldnow.com/images/6447754_G.jpg';
	$image_2 = 'http://WLEX.images.worldnow.com/images/6444267_G.jpg';
	$image_3 = 'http://WLEX.images.worldnow.com/images/6444268_G.jpg';
	$image_4 = 'http://WLEX.images.worldnow.com/images/6444269_G.jpg';
	//
	// The time stamps
	//
	$time_1 = 'wnRenderDate(\'Wednesday, January 14, 2015 9:46 PM EST\'';
	$new_time_1 = 'wnRenderDate(\'Friday, April 1 2016 12:00 AM\'';
	$time_2 = 'wnRenderDate(\'Thursday, January 15, 2015 8:39 AM EST\'';
	$new_time_2 = 'wnRenderDate(\'Friday, April 1 2016 12:00 AM\'';
	//
	// Change Weapons
	//
	$weapon = 'weapon';
	$rocket_launcher = 'rocket launcher';
	//
	// Get away cars
	//
	$toyota = 'White Toyota 4Runner';
	$lambo = 'Lamborghini';
	//
	// Time of the incident
	//
	$time_incident = '4:00 p.m. Wednesday';
	$new_time_incient = 'Midnight April Fools Day';
	/*
	Create the array for 
	keys and values in a 
	variable
	*/
	$find_replace = [ // Let's start our array
					// Full Name
					'John M. Nation' => 'Jarod E. Thornton', 
					// Alias
					'Johnnation' => 'Jarodthornton',
					// Last Name
					'Nation' => 'Thornton', 
					// First Image
					'<img src="'. $image_1 . '"' => '<img src="my-image-1.jpg"',
					// Second Image
					'<img src="'. $image_2 . '"' => '<img src="ninjas.jpg"', 
					// Third Image
					'<img src="'. $image_3 . '"' => '<img src="my-image-2.jpg"', 
					// Fourth Image
					'<img src="'. $image_4 . '"' => '<img src="lambo.jpg"', 
					// Time stamp 1
					$time_1 => $new_time_1, 
					// Time stamp 2
					$time_2 => $new_time_2, 
					// Get away cars
					$toyota => $lambo, 
					// Time of the incident
					$incident => $new_time_incient, 
					// Change weapons
					$weapon => $rocket_launcher, 
					//
					// Now lets just rewrite some stuff
					//
					'<h3 class="">Lexington Police Searching For Bank Robbery Suspect</h3>' 
					=> 
					'<h3 class="">Lexington Police Searching For Bank Robbery Suspect Jarod Thornton</h3>',
					//
					'<p>Officials want to stress that the subject is armed.&nbsp;</p>' 
					=> 
					'<h3>Officials want to stress that the subject is armed.&nbsp;</h3><p>Jarod is highly trained in Karate, Aikido, Taekwondo,Brazilian Jiu-Jitsu, and is a skilled ninja. The photo on the right shows Thornton with a known partner in crime, Jarod Warren. Warren is also highly trained in martial arts. If the two are encountered together, run, just run, fast and far away.</p>', 
					//
					'<p>If you have any information about Thornton\'s whereabouts, contact Lexington Police at 859-258-3600</p>' 
					=> 
					'<p><strong>If you have any information about Thornton\'s whereabouts, contact Lexington Police at 859-258-3600</strong></p><p><small>If you want to see how I did this, visit <a href="" target="_blank">Link here</a>. Thanks for checking out my page. I hope you enjoyed!',
					]; // Closed the array
//////////////////////////////////////////
	//
	// This is where the magic happens
	//
	// Now we take our array of each key and value
	foreach ($find_replace as $key => $value){
		// Will look for matched keys and replace them with our new values 
	$pull = str_replace($key, $value, $pull); 
	}
//	
// Ready, set, echo!
//
echo $pull;

Contact us to learn more about Adopt the Web for your business

Author Jarod Thornton

More posts by Jarod Thornton