HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media


Welcome to TinyPortal. Please login or sign up.

July 21, 2024, 07:50:23 AM

Login with username, password and session length
  • Total Posts: 195,333
  • Total Topics: 21,233
  • Online today: 103
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 45
  • Total: 45

[Block] Daily Dilbert Cartoon

Started by freddy888, March 25, 2010, 03:01:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


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.


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 -
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 = "";

// 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';


Cool, I'll be trying this one on the new beta. ;)