Login  |  Register
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media


Welcome, Guest. Please login or register.
Did you miss your activation email?

February 05, 2023, 09:44:35 AM

Login with username, password and session length
  • Total Members: 3856
  • Latest: Bixby
  • Total Posts: 193973
  • Total Topics: 21135
  • Online today: 40
  • Online ever: 3540
  • (September 02, 2022, 06:38:54 PM)
Users Online
Users: 4
Guests: 29
Total: 33

Author Topic: [Block] Daily Dilbert Cartoon  (Read 9436 times)

0 Members and 1 Guest are viewing this topic.

Offline Freddy

  • Moderator
  • *
  • Posts: 1617
    • AiDreams
[Block] Daily Dilbert Cartoon
« on: March 25, 2010, 09:01:40 AM »
Name of Snippet: Daily Dilbert Cartoon
SMF/TP versions tested:
 - SMF 1.1.11  + TP 0.9.8
 - SMF 1.1.11 + TP v1.0.5 beta 1
 - SMF 2.0 RC2 + TP v1.0 beta5
Block Type: PHP
Author: Freddy888 and an unknown coder
Link to Discussion: Click here....
Requirements: Nothing other than a sense of humour.

Here's something I have been working on for my site the past few days, I thought I would share it.

Basically it pulls in the Dilbert RSS feed and strips out everything apart from the cartoons - here's hoping it's okay to do that, but hey isn't RSS supposed to be like that - getting your content onto other sites...

Anyway, as well as pulling cartoons I also made the cartoon click-able so that it takes you to the Dilbert main site - hopefully that is courtesy enough.  They do provide widgets there as well but I wanted something a little less flashy and plain.

You can pull more than one cartoon if you like by changing $cartoonLimit , but as it is, it will pull the most recent - it looks like they update it daily.

The RSS reading code is from an author unknown, I think I found it on TP a long time ago.  The modifications by me simply tidy things up as there was a lot of stuff in the RSS feed that wasn't needed.

Coders may notice that it actually reads the whole feed and doesn't break out after say the first cartoon - I may work on that, but the feed is pretty small in size, so it's no biggie.


Oh and and you can See it in action here...

v1.1 - streamlined a little.
v1.2 - stops conflict with other RSS readers using same variables on same page.
« Last Edit: April 23, 2010, 06:28:14 AM by freddy888 »

Offline Freddy

  • Moderator
  • *
  • Posts: 1617
    • AiDreams
Re: [Block] Daily Dilbert Cartoon
« Reply #1 on: March 25, 2010, 09:03:09 AM »
Code: [Select]
A PHP block with code that accesses the Dilbert RSS feed and returns
a set number of cartoons.
Original RSS feed code author unknown.
Adapted by freddy888 March 2010
@ Tiny Portal - http://www.tinyportal.net/index.php?topic=32155
Version 1.2

global $cartoonCount, $cartoonLimit;

// Set this to how many cartoons you want to display.
$cartoonLimit = 1;

// Set the feed where Dilbert is located.
$rssFeed = "http://feeds.dilbert.com/DilbertDailyStrip?format=xml";

// Will count the number of cartoons found.
$cartoonCount = 0;


function DIL_start_Element($xp, $name, $attributes)

global $item,$currentElement;  $currentElement = $name;
//the other functions will always know which element we're parsing 
if ($currentElement == 'ITEM')
//by default PHP converts everything to uppercase   
$item = true;
// We're only interested in the contents of the item element.
// This flag keeps track of where we are 

function DIL_end_Element($xp,$name)

global $item, $currentElement, $title, $description, $link, $pubdate, $cartoonCount, $cartoonLimit;

// Are we at the end of the item element ?
if ($name == 'ITEM')
// Are we below the cartoon limit, if we are display the cartoon.
if ($cartoonCount < $cartoonLimit)
// Remove and tags in the title.   
$title = strip_tags($title);

// Remove everything apart from images.
$description = strip_tags($description, '<img>');
// Find the  end of the first image which is the cartoon.
$firstImageEnd = stripos($description, '>');
// Extract the cartoon image only, which happens to be first.
$cartoon = substr($description, 0, $firstImageEnd + 1);
// Insert alt and title attributes
$cartoon = str_replace('/>', 'alt="Dilbert ' . $title . '" title="Dilbert ' . $title . '" />', $cartoon);
// Display the cartoon/
echo '
<div style="margin: 15px; text-align: center">
<a href="' , $link , '" target="_blank">' , $cartoon , '</a>


// Reset globals
$title = '';   
$description = '';   
$link = '';
$pubdate = '';
$item = false;
$cartoonCount += 1;

function DIL_characterDataHandler($xp,$data)

global $item, $currentElement, $title, $description, $link, $pubdate;   

if ($item)
//Only add to the globals if we're inside an item element.   
case "TITLE":       
$title .= $data;
// We use .= because this function may be called multiple
// times for one element.       

case "DESCRIPTION":       

case "LINK":       

case "PUBDATE":

function DIL_readFeeds($feed)
$fh = fopen($feed,'r');
// open file for reading

$xp = xml_parser_create();
// Create an XML parser resource

xml_set_element_handler($xp, "DIL_start_Element", "DIL_end_Element");
// defines which functions to call when element started/ended

xml_set_character_data_handler($xp, "DIL_characterDataHandler");

while ($data = fread($fh, 4096))
if (!xml_parse($xp,$data))
return 'Error in the feed';
« Last Edit: March 25, 2010, 11:05:05 AM by freddy888 »

Offline Skhilled

  • Beta Tester
  • *
  • Posts: 1179
  • Once you stop learning, you stop living!
    • Doc Skillz
Re: [Block] Daily Dilbert Cartoon
« Reply #2 on: March 26, 2010, 07:32:58 AM »
Cool, I'll be trying this one on the new beta. ;)

Reading can seriously damage your ignorance!