Sorting Arrays By Length in PHP

However, in the order given, the color names near the beginning of the array were shorter than the ones near the end of the array; for example, “green” came before “darkgreen.” As a result, after replacement occurred, “darkgreen” became “dark#4b4b4b” (by replacing only the “green” part with a hex equivalent, instead of the entire “darkgreen” part) instead of the darkgreen hex equivalent. Obviously this was unacceptable, so the easiest way to fix the problem was to sort the array in such a way that the shorter names occurred last. Fortunately, PHP allows you to create custom algorithms for sorting arrays, and that’s just what I’ve done.

  1. function sortByLength($a,$b){
  2. if($a == $b) return 0;
  3. return (strlen($a) > strlen($b) ? -1 : 1);
  4. }

Simple, yes, but effective.

Update: This code (written when I was more of a PHP novice) is erroneous. The following function is ideal. As mentioned in the comments, call this function using the PHP usort() (or similar callback) function.

  1. function sortByLength($a,$b){
  2. return (strlen($a) - strlen($b));
  3. }

March 24th, 2006 | 3 Remarks

Thoreau

Henry David Thoreau was a transcendentalist writer of the 19th century who I encountered recently in school. Although I am in strong conflict with many of his ideas, in his journal he makes some excellent points on how to write better. I believe these concepts are also, to some extent, applicable to (web) design.

The writer must to some extent inspire himself. Most of his sentences may at first lie dead in his essay, but when all are arranged, some life and color will be reflected on them from the mature and successful lines; they will appear to pulsate with fresh life, and he will be enabled to eke out their slumbering sense, and make them worthy of their neighborhood. In his first essay on a given theme, he produces scarcely more than a frame and groundwork for his sentiment and poetry. Each clear thought that he attains to draws in its train many divided thoughts or perceptions. The writer has much to do even to create a theme for himself. Most that is first written on any subject is a mere groping after it, mere rubble-stone and foundation. It is only when many observations of different periods have been brought together that he begins to grasp his subject and can make one pertinent and just observation.

In regard to design, Thoreau’s thoughts can be translated like so:

The designer must to some extent inspire himself. Most of his design elements may at first lie dead in his mind, but when all are arrangd, some life and color will be reflected on them from the mature and successful changes; they will appear to pulsate with fresh life, and he will be enabled to eke out their slumbering sense, and make them worthy of their neighborhood. In his first design (mockup) for a given job, he produces scarcely more than a frame and groundwork for his sentiment and ideas. Each clear thought that he attains to draws in its train many divided thoughts and perceptions. The designer has much to do even to create a design for his blog. Most that is first developed on any design is a mere imagination, mere thoughts and basic ideas. It is only when many observations of different changes have been brought together that he begins to grasp the completed design and can make one pertinent and just observation.

In another journal entry, Thoreau makes these observations…

In the composition it is the greatest art to find out as quickly as possible which are the best passages you have written, and tear the rest away to come at them. Even the poorest parts will be most effective when they serve these, as pediments to the column…

Again, the designer’s translation would be something like the following…

In the design it is the greatest art to find out as quickly as possible which are the best design elements you’ve created, and tear away the excess so that you can focus on the best ideas first. Even the ideas which seemed poor in the beginning may turn out to be very effective when placed appropriately, much like the pediments (broad triangular parts above doors or columns) to the column.

The last journal entry that I read of Thoreau’s goes like this:

Find out as soon as possible what are the best things in your composition, and then shape the rest to fit them.

This one doesn’t even need to be changed, as it is perfectly applicable to the designer as is. The basic point here is the same as the previous paragraph: find out the best thing in your first mockup, and strip away all other elements until you’ve created what’s best. Then add the other elements in, and they may (or may not) look good, but (going all the way back to the first paragraph) at that point you can make a pertinent observation whether those elements look bad, and if so strip them away.

March 21st, 2006 | Remark

Yahoo Mail UI Design

However, one flaw that has caused much inconvenience is the incredibly tiny “Empty” link that deletes spam from the Bulk folder. Almost half of the time, when I go to click the “Empty” link, I end up clicking the area that surrounds it, and wind up viewing the Bulk folder instead of emptying it. This is exceedingly frustrating. Here’s how Yahoo’s interface currently looks:

[The Yahoo Mail interface with the bulk folder link highlighted.]

Yahoo should at the very least add some padding to the “Empty” link. It may be desirable to have it small to avoid accidental clicking, but their confirmation message serves to prevent that, so there is no need to keep the link smaller. My suggestion would be to take the Bulk folder’s link and cut its width in half, and use the other half of the space that the Bulk folder’s link normally consumes for the “Empty” link, so the two links would be alongside each other and both would be easy to click.

March 18th, 2006 | 4 Remarks

The Google-Yahoo Effect

There are advantages to both sides: Yahoo knows exactly what Google is offering and can offer something uniquely different, albeit in the same product. For example, we saw how much attention Google got when they opened up Google Maps. The way Google’s system was built is completely different than in the past, and the idea of online maps was not from any one company’s ideas exclusively — both MapQuest and Yahoo had online maps at the time, but Google copied neither’s ideas. Google brought online maps to the next level, and shortly thereafter, our friends over at Yahoo reconstructed their system (in Flash, no less) to rival Google’s system. Google, on the other hand, can claim that they were the creators of this higher level of mapping, and have a head-start on Yahoo.

Of course, since Yahoo has been around longer, many of Google’s ideas may appear to be from Yahoo, but we’re not looking at the big picture. Online mapping was developed by more than one single company, as was email, online storage, web searching, and many other things that Google offers. Regardless, Google has taken ideas which, in some cases, have garnered limited success in the past and turned them into practical, though possibly over-hyped, applications.

You might say Google is copying other companies’ ideas in the same way that Yahoo is copying Google’s ideas (or their own ideas after being influenced by Google’s changes to them), but what makes it so obvious that Yahoo is intensely watching Google and jumping on any opportunities to offer the same products, is how Yahoo closely follows behind Google. It was just a few months following the release of Google Maps that Yahoo Maps was reconstructed, and it was just a few weeks following the announcement of GMail that Yahoo (and MSN) expanded their email storage capacity.

In Yahoo’s defense (which I admit, due to my biased point-of-view, I will attempt to make as washed-up and worthless as possible), if Yahoo had not closely rebuilt Google’s recently released products, Yahoo would certainly be falling further behind in the game. Yahoo’s decisions make perfect economic sense, but one of the ramifications is the appiration that Yahoo is simply copying Google. And unfortunately for Yahoo, the public gets this impression, too.

To illustrate that the public gets the aforementioned impression, I’d like direct your attention to the following snippet of a conversation I had with a friend a few days ago.

Me: I call it the “Google-Yahoo Effect”
Me: Has a nice ring to it, eh?
Friend: A bit
Friend: What is it about? Google makes something innovative and then Yahoo copies it and tries to beat it?

Hence the two companies, in all fairness, are battling over who has the better product line, and for now, the winner is Google. It also seems that the one thing Yahoo forgot to copy may be one of the most critically important things they could have done to maximize revenue early in the game — before it was too late — and that thing is called advertising. (Okay, they have advertising, but you never hear anyone talking about “Yahoo ads” do you? I think that, to be consistent, Yahoo should have competed with Google more in this area than they did. Who knows? Yahoo may have become a major advertising competitor at this point.)

Taking ideas that are innovative, new, old, unsuccessful or successful, reinventing it to become even better, and having a rival follow in your footsteps: this is the Google-Yahoo Effect.

March 16th, 2006 | 4 Remarks

Ajax Toybox

Justin’s examples include an ajax-powered clock, RSS news feed ticker, calculator, and city/state look-up. You can check out the demo or read what Justin has to say about it. Thanks, Justin!

March 13th, 2006 | 2 Remarks

Netless

Well, I have a message for anyone whose laptop has ceased to function like that. Check the button that disables networking!

This has been a message from experience.

March 9th, 2006 | Remark

GrayBit Beta

So What’s GrayBit?

Basically, it’s a PHP webapp that converts any web site into black-and-white. That’s right, even your images. This provides a great way to tell if your site has too high or too low of a contrast between its elements, and best of all (unlike other similar tools), GrayBit is interactive.

It doesn’t Work

This being the beta launch, we’re still ironing out some flaws in the system. Overall, though, GrayBit performs very well. If you’re trying a site like Yahoo, Amazon, or Google, the results will be mixed at best, due to the fact that those sites use tables for layout and GrayBit is designed with its target audience — accessibility-aware developers — in mind. Ergo, we’ve chosen to make the system optimal for our target audience, rather than trying to be everything for everybody (really, no one who makes a site like Amazon is going to care what their site looks like in black and white — they already obviously don’t care what it looks like with color!)

Special Thanks

I just wanted to take a minute and thank Bokeh from the Web Developer Forums for some help with the PNG processing functions. I was at a loss there for a couple days, unable to figure out how to get alpha transparency to work properly (due to my lack of experience with PHP and GD). Thanks to Bokeh, I was able to figure it out.

Now, for any further information, please direct your (undivided) attention to http://GrayBit.com/, where the fun goes vibrant in a completely colorless way!

March 7th, 2006 | Remark