About the Author

Chris Shiflett

Hi, I’m Chris: entrepreneur, community leader, husband, and father. I live and work in Boulder, CO.


PHP Tidbits

I'm developing a new web site for shiflett.org from the ground up, focusing on a clean, accessible design. As a result, I've been noticing all of the things I dislike about blogs, mine included. Navigation, commenting, and community are some aspects that I especially hope to improve.

I must admit, though, that instead of diving right in, I've been goofing off. Just for fun, I'd like to share a couple of quick PHP tidbits with you that I wrote instead of starting on the real project at hand. :-)

The first is an example that really shows off how useful a simple REST API can be in combination with SimpleXML. I've been using FeedBurner for a while for my feed, and it's cool to see how many people are subscribed. As part of the new design, I'd like to be able to grab that number without having to use their image. Enter the FeedBurner Awareness API. With two lines of PHP, I'm good to go:

<?php 

$info 
simplexml_load_file('http://api.feedburner.com/awareness/1.0/GetFeedData?uri=shiflett');
$subscribers $info->feed->entry['circulation'];

?>

Formatting can be difficult when you have really long URLs, and one of the best solutions I've seen is to shorten URLs to just the first x characters and the last y characters. Something like this:

<?php 

function shorten_url($url$separator '...'$first_chunk_length 35$last_chunk_length 15)
{
    
$url_length strlen($url);
    
$max_length $first_chunk_length strlen($separator) + $last_chunk_length;

    if (
$url_length $max_length) {
        return 
substr_replace($url$separator$first_chunk_length, -$last_chunk_length); 
    } 

    return 
$url;
}

$url 'http://averylongdomainname.org/a/very/long/path/to/averylongfilename.pdf';
$short_url shorten_url($url);

?>

With this, you can link to $url and display $short_url, and it's still pretty clear where the link takes you. Of course, you can also easily adapt it to fit any particular length, and you can even use a real ellipsis instead of ... for the separator.

I'm currently writing a test suite for comments, since I want to allow more formatting in the comments as well as maintain strict XHTML. I'm hoping to find an existing solution, but I haven't found anything yet. Once I have my criteria better defined and a decent test suite written, I'll blog more about it.

About this post

PHP Tidbits was posted on Tue, 24 Oct 2006. If you liked it, follow me on Twitter or share:

11 comments

1.Pete said:

Hi, Chris -

I am using IE 6.0 and this page (all of your blog pages that I've seen) doesn't appear to show the columns correctly. In addition, this center column is cut off at the left. (i.e. when I enter "Chris" above, I can only actually see "is").

Pete

Wed, 25 Oct 2006 at 03:42:22 GMT Link


2.Matthijs said:

Looking forward to the site Chris. Sounds like a good plan.

For the comments formatting, have you seen the HTML purifier project from Ambush Commander http://hp.jpsband.org/, see also http://forums.devnetwork.net/viewtopic.php?t=54805&highlight=htmlpurifier?

Wed, 25 Oct 2006 at 05:28:58 GMT Link


3.Dave Marshall said:

I get the same in IE6 for this page, column flow is out and the center column gets chopped. Looks good in IE7 though, except for the date which is slightly chopped across the top. Nevertheless, I like the new design and the simple feedburner tip, cheers Chris.

Wed, 25 Oct 2006 at 07:49:43 GMT Link


4.Peter Mescalchin said:

Look forward to seeing your results.

As for comments system - maybe you should take a look at "Markdown"?

http://daringfireball.net/projects/markdown/

One of the keys to implementing a markup system for a CMS/blog system while maintaining XHTML validity is to ensure tags are correctly nested - no good having your bold tags, closed before your italics/etc. Sadly alot of markup systems I have seen simply do regexp replaces and don't take this into consideration.

The solution, which was hinted to me by a blog post on Simon Wilson's weblog:

http://simon.incutio.com/archive/20...OfMarkupSystems

The solution is to use recursive regexps, which while being a little more complicated to implement and a little more overhead - will do the trick and ensure only correctly nested markup renders. I have implemented such a system for some of my newer website releases for clients with great success.

http://simon.incutio.com/archive/20...ystems#comment7

Wed, 25 Oct 2006 at 09:12:38 GMT Link


5.Jon Tan said:

Hi Dave and Pete,

This is not the new design - it's on its way (I'm working on it with Chris) so please bear with us. Rather than fixing the current site we're concentrating on the new version with some new features hopefully you'll like.

In the meantime it may be worth using an alternative browser that doesn't have IE's CSS bugs. The redesign will render properly on Win/IE6. Or rather, we will workaround IE's incorrect CSS support to make it do what it is supposed to. :)

All the best

Jon

Wed, 25 Oct 2006 at 13:14:39 GMT Link


6.Chris Shiflett said:

Pete, thanks for letting me know, and sorry about that. The new design will work in all browsers, so I hope you can tolerate this one for a few more weeks.

Matthijs, thanks for those links. Once I can get my criteria written down in a coherent list, I want to go through all available options. I'll definitely check this out. :-)

Dave, glad to hear it works in IE 7. Just to be clear, though, this is not the new design. It's coming.

Peter, thanks for the links. I've considered markdown, but I'm not sure I want people to have to learn yet another markup language just to be able to format their comments.

Wed, 25 Oct 2006 at 13:20:10 GMT Link


7.Ben Ramsey said:

I use the Feedbuner Awareness API on my site to actually create a look-alike feedburner image to fit my template. It's nifty:

http://benramsey.com/archives/creat...-from-xml-data/

Wed, 25 Oct 2006 at 14:59:02 GMT Link


8.Hannes Magnusson said:

$short_url = substr_replace($url, "...", 35, -15);

Wed, 25 Oct 2006 at 20:07:00 GMT Link


9.Chris Shiflett said:

Thanks, Hannes. I forgot all about that function.

Wed, 25 Oct 2006 at 20:15:03 GMT Link


10.Edward Yang said:

Author of HTML Purifier here.

Quite frankly (and I don't mean to put down my product) I don't think HTML Purifier by itself would be a good idea for the job. It's more of a problem of scope: HTML Purifier is designed to let as much HTML as possible through. That means users are free to color their text blue, insert tables of data, create floated figures, link to external images, etc. HTML Purifier is great if you want an HTML parser to let these things through, but I don't think that's what you're aiming for with blog comments. I view this limitation of HTML Purifier as a bug, and I'm working on fixing it.

But as the links by Peter Mescalchin note, Markdown does not produce 100% XHTML compliant code. In this way, HTML Purifier can come back into play: pass the Markdown-produced HTML through HTML Purifier, and voila, guaranteed XHTML validation. I'm not convinced, however, that Markdown limits its tags in a satisfactory way either (why would a blog comment need headings?)

In any case, if you're not going to use a WYSIWYG editor (if you do, use HTML Purifier!), be sure not to have the input format as raw HTML: that's quite user unfriendly, even for those of us who are used to writing HTML (for instance, Slashdot allows pure HTML input. I never use it, because I always forget to wrap my text in paragraph tags)

Wed, 25 Oct 2006 at 23:20:16 GMT Link


11.Chris Shiflett said:

Hi Edward,

Thanks for stopping by. I'm actually writing some code right now, just to force myself to explore this problem more thoroughly before trying to come up with a specific set of criteria.

I'll definitely be checking out HTML Purifier.

Thu, 26 Oct 2006 at 03:22:25 GMT Link


Hello! What’s your name?

Want to comment? Please connect with Twitter to join the discussion.