I have a site where people will submit text. I want to add a couple of attributes to links that are added. Anyone know a regular expression (or another good way) to add rel="nofollow" to any link that is in the body of the text that is submitted?
+1
A:
Use DOM parsing (PHP Simple HTML DOM Parser example):
// Create DOM from string
$html = str_get_html($links);
$linksCount = count($html->find('a'));
for($i=0;$i<$linksCount;$i++) {
$html->find('a', $i)->rel = 'nofollow';
}
echo $html;
karim79
2009-09-23 18:13:15
Thanks. That looks like a pretty descent solution.
Brandon Hansen
2009-09-23 18:18:40