Replace some text matches with url link with text as anchor text

By Allen Stoner

Do you want to replace text on your web site with a link to a url, but don't want to do it everytime? An application of this could be everytime someone's name appears to replace it with a link to their bio page. To take this to the next level the attached code accepts a parameter to determine the chance that a particular match has of getting replaced, so it is possible to only replace 80% of the matches.

<?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/>';
?>

Replace some text matches with url link with text as anchor text  (1741 Views)