$str = stripslashes($str);
$preg = "/<[s]*a[s]*href=[s]*["']?([w.-]*)["']?[^>]*>(.*?)/i";// the preg variable matches html charactersyou are looking for
if(preg_match_all($preg, $str, $match))
{
// the preg_match_all() function takes the $preg code and uses it to find every instance of the match
foreach ($match[1] as $key=>$val)
{
// foreach just loops through all the instances
if(isset($match[0]))
{
$txt = $match[0][$key];$link = explode(""",$txt);$linktext = $match[2][$key];// in this situation we used the explode() function to break up the anchor tag at the double quotes to find the href url
$pattern[] = '/'.preg_quote($match[0][$key],'/').'/';
$replace[] = "<a href="$link[1]" rel="nofollow" target="_blank">$linktext</a>";// in this example we wanted to change any matches to be a no-follow link and put the string back together to feed the string back into the program
}
}
preg_replace($pattern, $replace, $str);// one thing to make note of is that in this example if no link matches are found it will not return anything to your program
// you should add that if anything is not matched just return the original string back to your program
}