Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. function __WrappingTags($html)
  2. {
  3. function isExternalLink($link)
  4. {
  5. $link = parse_url($link);
  6. if(isset($link["host"]))
  7. if($link["host"]=="3data.ru")
  8. return false;
  9. else
  10. return true;
  11. else
  12. return false;
  13. }
  14.  
  15.  
  16. //$html = "Текст <a href='http://yandex.ru/page1.html'>ссылка</a> bbb Текст <a href='http://3data.ru/page1.html'>текст</a> и снова <a href=\"page2.html\" title=''>еще ссылка</a> конец";
  17.  
  18. $nofollow = ' target="_blank" rel="nofollow';
  19. $prefix = "<!--noindex-->";
  20. $postfix ='<!--/noindex-->';
  21.  
  22. preg_match_all("/<[Aa][\s]{1}[^>]*[Hh][Rr][Ee][Ff][^=]*=[ '\"\s]*([^ \"'>\s#]+)[^>]*>/", $html, $matches);
  23. $changed = $matches[1];
  24. $toreplace = $matches[1];
  25.  
  26. for($i=0; $i<count($changed); $i++)
  27. if(isExternalLink($changed[$i]))
  28. $changed[$i] = $changed[$i] . '"' . $nofollow;
  29. else
  30. $changed[$i] = $matches[1][$i];
  31. $html = str_replace($toreplace, $changed, $html);
  32.  
  33. preg_match_all("/<[Aa][\s]{1}[^>]*[Hh][Rr][Ee][Ff][^=]*=[ '\"\s]*([^ \"'>\s#]+)[^>]*>/", $html, $matches);
  34. $change = $matches[0];
  35. $rep = $matches[0];
  36.  
  37. for($i=0; $i<count($change); $i++)
  38. if(isExternalLink($matches[1][$i]))
  39. $change[$i] = $prefix . $rep[$i] ;
  40. else
  41. $change[$i] = $matches[0][$i];
  42. $html = str_replace($rep, $change, $html);
  43.  
  44. preg_match_all("/<a.+?href[^=]*=[ '\"\s]*([^ \"'>\s#]+)[^>]*>.*?<\/a>/", $html, $new);
  45. $secondpart = $new[0];
  46. $replace = $new[0];
  47.  
  48. for($i=0; $i<count($secondpart); $i++)
  49. if(isExternalLink($matches[1][$i]))
  50. $secondpart[$i] = $secondpart[$i] . $postfix;
  51. $html = str_replace($replace, $secondpart, $html);
  52.  
  53. return $html;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement