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>


