Skip to content

Instantly share code, notes, and snippets.

@pankamilr
Created June 8, 2015 09:18
Show Gist options
  • Save pankamilr/8142b6ee7630dfe2d140 to your computer and use it in GitHub Desktop.
Save pankamilr/8142b6ee7630dfe2d140 to your computer and use it in GitHub Desktop.
Add rel nofollow to all external links in given content. $skip contains domain name
public function nofollow($html, $skip = null) {
return preg_replace_callback(
"#(<a[^>]+?)>#is", function ($mach) use ($skip) {
return (
!($skip && strpos($mach[1], $skip) !== false) &&
strpos($mach[1], 'rel=') === false
) ? $mach[1] . ' rel="nofollow">' : $mach[0];
},
$html
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment