-1

Would be possible with php and one regular expression to do the following. Get all hyperlinks inside of a content and rewrite them if the top level domain of hyperlink is matching a given tld name from array.

Now have a regular expression which rewrites all hyperlinks in a given content

preg_replace('/<a(.*)href="([^"]*)"(.*)>/','<a$1href="goto.php?url=$2"$3>', $content);

example

$tld = array("http://www.example.com","http://www.test.com");

if <a href="www.example.com">example</a> than <a href="/goto.php?url= www.example.com"</a>;
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
lgt
  • 1,024
  • 3
  • 18
  • 33

1 Answers1

1

You might want to solidify your regex a bit...

$pattern = <<<EOL
/<a([^>]+)href\s*=\s*(['" ]?)([^"'> ]*)(['" ]?)([^>]*)>/si
EOL;

$replacement = "<a$1href='goto.php?url=$3'$5>";

preg_replace($pattern, $replacement, $content);

Not able to test this right now, so there may be a typo...

HappyTimeGopher
  • 1,377
  • 9
  • 14