This Is Really Empty – The Daily WTF

Date:

Share:

Konrad was trying to understand how an input form worked, and found this validation function.

function IsReallyEmpty($subject)
{
        $trimmed = trim(preg_replace("/&.*;/", "", $subject));
        return strlen($trimmed) != 0;
}

Now, I can understand why one might want to have a different definition of “empty” when it comes to strings. An all whitespace string, like " " may rightfully be considered an empty input for many applications.

So calling trim makes a lot of sense. It’s the preg_replace that starts to worry me, because that regex is clearly trying to match an HTML entity, aka  . But it matches all HTML entities, not just ones like   which are whitespace characters, but ampersands and greater/less-than signs.

But there’s another problem with the regex. The * operator is greedy. So  Hello World  would see the opening &, the closing ; and decide the entire string could be rejected.

But that’s not the real WTF. The real WTF is the very last line. In a function called IsReallyEmpty, it returns true if the input string is not empty, thus stretching the definition of “really” to new levels.

“Is this string really empty?” “No, it is.”

[Advertisement]
BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!

Source link

Subscribe to our magazine

━ more like this

Postcards & Emails – PostSecret

—–Email—–—–Frank@PostSecret.com—– Dear Frank, Last week you were speaking in Portland, Oregon.  I walked in a little late to a very crowded room. You said a few...

How Anna Wintour’s Vogue front covers made a statement to the end | Anna Wintour

During her 37-year tenure as editor-in-chief of American Vogue, Anna Wintour has presided over more than 400 covers. December 2025’s, on newsstands this week,...

Painted by Esther’s Journey From Art School to Hollywood

While each product featured is independently selected by our editors, we may include paid promotion. If you buy something through our links, we may...

Beware These Black Friday Shopping Scams

Holiday shopping season is ripe for scammers, as...

‘We stayed in a 500-year-old palazzo for €100’: readers’ favourite historic places to stay in Europe | Hotels

An opulent stay in VeniceMy husband and I stayed in a beautiful 500-year-old Venetian palazzo for just €100 for a double room. The exterior...