<?php
class ReplaceClass
{
// property declaration
public $random = 100;
public $name = '';
public $url = '';
public $newWindow = '';
// method declaration
public function randomreplace($matches) {
// some of the time, return replace text
if (rand(1,100) <= $this->random) {
return '<a'.$this->newWindow.' href="'.$this->url.'">'.$this->name.'</a>';
};
return $matches[0];
}
}
function doSpin($content, $random, $name, $url, $newWindow, $wordBoundary) {
$a = new ReplaceClass();
$a->random = $random;
$a->name = $name;
$a->url = $url;
$a->newWindow = $newWindow;
$spun = preg_replace_callback('@('.$wordBoundary.$name.$wordBoundary.')(?!([^<>]*<[^Aa<>]+>)*([^<>]+)?</[aA]>)(?!([^<\[]+)?[>\]])@',
array(&$a, 'randomreplace'), $content);
unset($a);
return $spun;
}
$content = 'This should make the linked words appear much more random. At least
I think it should.';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 50, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 60, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 70, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
echo doSpin($content, 100, 'should', 'http://www.eggheadcafe.com', ' target="_blank"',
'\b').'<br/>';
?>