Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Display W3C Tip

Display W3C Tip

Ever want to display the tips from the W3C page? Well now you can!

Here you go, I commented each line, if you have a question don't hesitate to ask.


// The URL To open for parsing 
$url = "";

// attach the file to a variable
$rawLines = file($url);

// create an array
$lines = array();

// set the event counter to -1 to offset the array
$lineNo = -1;

// loop through each line in the file and pull out the intended lines
foreach ($rawLines as $rawLine) 

	// First search for the line
	if (strstr($rawLine, '<h2 id="quality">') && strstr($rawLine, '</h2>')) 

		// If it is found continue or go to the elseif statement
		strstr($rawLine, '<h2 id="quality">Quality Tips</h2>') ? 
                $parse = true : $parse = false;

	// Look for the second string
	elseif ($parse == true && strstr($rawLine, '<li class="tip">')) 

		// Increment the lineNo variable

		// Add the tip string to an array by breaking apart the string. 
		$lines[$lineNo]["L"] = substr($rawLine, $start = strpos($rawLine, 
                '<li class="tip">') + 0, strpos($rawLine, '</a></li>') - $start);

// Create another array to hold the individual tips
$tips = array();

// Set the count variable

// Now loop through each tip and go through a series of "str_replace" to clean up the string
foreach ($lines as $line)	{
	$tip = str_replace('<li class="tip">','',$line["L"]);
	$tip = str_replace('<a href="','',$tip);

	// Add the " | " to the string to later explode it for a cleaner result
	$tip = str_replace('">',' | ',$tip);

	// Now break apart the tips into the tip array using " | " as a delimiter
	$tip = explode(" | ", $tip);
	// Create the 2 arrays and "push" the text and links in to them
	$tip_url[]	= "" . $tip[0] . "\n";
	$tip_txt[]	= strip_tags(html_entity_decode($tip[1])) . "\n";

// Create a random number. Seed the randomizer with the number of tips "$i"
$rand = mt_rand(0, $i);

// Return a random tip
print "<a href=\"" . $tip_url[$rand] . "\">" . $tip_txt[$rand] . "</a>";


That is the end of the code snippet. Now you have a script that will display a random tip from the w3c site.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Display W3C Tip

 Jonathan  Mon Sep 1, 2008 5:55 am  
It's not working, i copied and past the code in a PHP file.
Add a comment
The ability to add new comments has been disabled in this archived version.

Main | Tutorials | Articles