Don’t use strlen()

Each time I see someone use strlen() I cringe. It will break.

Despite its name, strlen() doesn’t count characters. It counts bytes. In UTF-8 a character may be up to four bytes long.

So what happens if we use strlen() and its companion substr() to shorten the title of post?

<?php # -*- coding: utf-8 -*-
declare( encoding = 'UTF-8' );
header('Content-Type: text/plain;charset=utf-8');

$string = 'Doppelgänger';
print 'strlen():    ' . strlen( $string ) . "\n";
print 'mb_strlen(): ' . mb_strlen( $string, 'utf8' ) . "\n\n";

print 'substr():    ' . substr( $string, 0, 8 ) . "\n";
print 'mb_substr(): ' . mb_substr( $string, 0, 8, 'utf8' );

Output:

I have to use an image here. If I had used the plain text output our newsfeed would break. And that’s what happens each time you use strlen() and substr() on strings encoded in UTF-8: You end up with partial characters and invalid UTF-8.

Alternatives for mb_strlen()

You can use different methods to get the real string length.

$length = preg_match_all( '(.)su', $string, $matches );

See also Hakre: PHP UTF-8 string Length.

Or just use …

$length = strlen( utf8_decode( $string ) );

There is also a nice php-utf8 library on GitHub from Frank Smit.

We’ve Lost A Very Good Friend, WordPress Enthusiast And An Important Part Of WPEngineer!

Sadness has descended on WPEngineer last week. Michael Preuß, one of our WPEngineer Team, died suddenly after a short illness and after a memorable life.

Frank and I are still in shock and we can't realize that Michael left us without saying goodbye. He leaves a big gap in our life. He was not only our teammate at WPEngineer, he also was a very good friend, who have shared the same love for WordPress and the community as we do.

We didn't meet us very often in person, since we live quite far away from each other. But when we did, we always had a blast and fun talking about WordPress and other stuff that was going on right now around the world. But most of the time we were talking on the phone for hours, literally.

This time our reunion was one of Frank's and my saddest moment. We had to carry Michael to his grave. It was painful, incomprehensible and unbearable at once. Unlikely for us, not one word about WordPress, this time we had to mourn the loss of our beloved friend Michael.

Feb 2009 - Matt meets WPEngineer: Frank, Michael, Matt, Alex

Feb 2009 - Matt meets WPEngineer: Frank, Michael, Matt, Alex

Just 4 years ago we got to know each other and became very good friends. Michael, Frank and I had the urge to give the WordPress community something back, a platform for writing tips, tricks and solutions for the WordPress community - WPEngineer was born! Frank and I will continue to publish future posts, despite the loss we have.

We will also help his partner and friend Heiko, to continue Michael's life's work Xtreme Theme.

He left his wonderful and caring wife Carola and his two sons Robert and Alexander behind, and I'm sure, many other WordPress Junkies, like we are, who still can't believe he is gone forever!

You will always be in our Source Code!

In deepest sympathy

Alex & Frank

Stop SOPA!

What is this all about: Help Stop SOPA/PIPA
Turn off your website on January 18th for 12 hours.

Simple and fast solution for turn the website off; use the Plugin WP Maintenance Mode, original for the activation of the maintenance mode; but its easy for this requirement. As useful; the plugin send an 503 Service Unavailable header.

Set the following settings in the plugin

Code for the field text, the video markup:

<iframe src="http://player.vimeo.com/video/31100268?byline=0&amp;portrait=0" width="400" height="225" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<p><a href="http://vimeo.com/31100268">PROTECT IP / SOPA Breaks The Internet</a> from <a href="http://vimeo.com/fightforthefuture">Fight for the Future</a>.</p>

Here is the result for not signed in visitors:

Simple Media Queries Tester


CSS Media Queries are getting more popular and the adjustment of the size of a website for different clients or the size of the browsers are more necessary. The Media Queries are a solution, which also have its advantages and disadvantages. It was a disadvantage, when using, that I couldn't see all sizes at the same time. So why not just a small script, the iframe element. If you like to use it, here is a simple solution or you can use the current script in my "Test-Laboratory".
Continue reading …

Simple Autoresize for WordPress Background Image Function


In WordPress 3.0 you have the possibility to easily upload and use a background image. The image is positioned via CSS in the background. Thus it is not quite simple to have the possibility of auto resize and the background adapts to the size of the browser. There are some nice possibilities for this request, I will briefly highlight a very simple way of using CSS, since then the function of WordPress will not be effected.

Continue reading …