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

Securely access Intercom data in ChatGPT

Customer conversations hold value for the entire business, but they’re often locked away in your support platform. From today, the new Intercom connector for ChatGPT...

The Rad pack: David Beckham leads country menswear trend | Men’s fashion

Does your wardrobe include a half-zip jumper and a flannel shirt? Is your fantasy car a vintage Land Rover Defender? Do you know a...

What Happens When You Marry A Beauty Person?

Richelle Marty, the COO of medical spa Plump, met her partner, Dom, in 2017 while she was trying to install infrared saunas at 11...

Chevy’s New Bolt Electric Car Is Truly Affordable, at Less Than $30,000

Chevrolet's new Bolt feels like the kind of electric car the market has been waiting for -- an EV that's genuinely affordable but doesn't...