Guest User

Untitled

a guest
Jun 21st, 2012
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. ## HTML ($content)
  2.  
  3. <p>Some text <a href=\"http://google.co.nz\" target='_blank'>link to Google</a>.
  4. Followed by <a href='http://example.com'>Another Link</a><img src='/some/image.png' alt='Some image'/>.
  5. <a href='mailto:[email protected]'>Email Me!</a></p>
  6.  
  7.  
  8. ## Function to deal with content
  9. <?php
  10.  
  11. public static function create_from_content($content, $source, $fullUrl) {
  12. //Create trackers for all <a> links
  13. $aReg = "/\<a.*?href=[\'\"](?:(?:^mailto)(\S*?))[\'\"].*?\>.*?\<\/a\>/";
  14. $aReplaceReg = "/(\<a.*?href=[\'\"])\S*?([\'\"].*?\>.*?\<\/a\>)/";
  15. if (preg_match_all($aReg, $content, $matches)) {
  16. $originals = $matches[0];
  17. $urls = $matches[1];
  18. $replacements = array();
  19. for ($i = 0; $i < count($urls); $i++) {
  20. $link = self::create($urls[$i], $source);
  21. $link->write();
  22.  
  23. $replacement = '$1' . $fullUrl . $link->Slug . '$2';
  24. $replacements[] = preg_replace($aReplaceReg, $replacement, $originals[$i]);
  25.  
  26. }
  27. $content = str_replace($originals, $replacements, $content);
  28. }
  29. //Create trackers for all <img>
  30. $imgReg = "/\<img.*?src=[\'\"](\S*?)[\'\"].*?\/\>/";
  31. $imgReplaceReg = "/(\<img.*?src=[\'\"])\S*?([\'\"].*?\/\>)/";
  32. if (preg_match_all($imgReg, $content, $matches)) {
  33. $originals = $matches[0];
  34. $urls = $matches[1];
  35. $replacements = array();
  36. for ($i = 0; $i < count($urls); $i++) {
  37. $link = self::create($urls[$i], $source, 'DirectDownload');
  38. $link->write();
  39.  
  40. $replacement = '$1' . $fullUrl . $link->Slug . '$2';
  41. $replacements[] = preg_replace($imgReplaceReg, $replacement, $originals[$i]);
  42.  
  43. }
  44. $content = str_replace($originals, $replacements, $content);
  45. }
  46. return $content;
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment