php WTF

The following code works:

<?php
class TestClass {
    static function test_method($foo) {
        global $x;
        return str_replace('twitter', $x, $foo);
    }
}

$x = 'wibble';

ob_start(
    function ($html) {
        $pattern = '~(<a .*?href=["\'])([^"\']+)(["\'].*?>.*?</a>)~';
        return preg_replace_callback(
            $pattern,
            function ($matches) {
                $y = TestClass::test_method($matches[2]);
                return $matches[1] . $y . $matches[3];
            },
            $html
        );
    }
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test for whether global variables work</title>
</head>
<body>
<article id="twitter">
<h1>Latest update from <a href="http://twitter.com/markfiend">my Twitter feed</a>:</h1>
<h2><a href="http://twitter.com/markfiend/statuses/304159221711241216">Wed 20 Feb 2013 09:23:04</a>:</h2>
<p>Case sensitivity is IMPORTANT: Consider: "I'm going to help Uncle Jack off a horse" and "i'm going to help uncle jack off a horse"</p>
</article>
</body>
</html>

However this does not:

<?php
class TestClass {
    static function test_method($foo) {
        global $x;
        return str_replace('twitter', $x->scalar, $foo);
    }
}

$x = (object) 'wibble';

ob_start(
    function ($html) {
        $pattern = '~(<a .*?href=["\'])([^"\']+)(["\'].*?>.*?</a>)~';
        return preg_replace_callback(
            $pattern,
            function ($matches) {
                $y = TestClass::test_method($matches[2]);
                return $matches[1] . $y . $matches[3];
            },
            $html
        );
    }
);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test for whether global variables work</title>
</head>
<body>
<article id="twitter">
<h1>Latest update from <a href="http://twitter.com/markfiend">my Twitter feed</a>:</h1>
<h2><a href="http://twitter.com/markfiend/statuses/304159221711241216">Wed 20 Feb 2013 09:23:04</a>:</h2>
<p>Case sensitivity is IMPORTANT: Consider: "I'm going to help Uncle Jack off a horse" and "i'm going to help uncle jack off a horse"</p>
</article>
</body>
</html>
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to php WTF

  1. Android operating systems significantly less than Android Version three are not supported due to functionality problems.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s