tags:

views:

34

answers:

1

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
Thanks. That looks like a pretty descent solution.
Brandon Hansen