url_parser.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /* URL Passing code to allow links from with in emails */
  3. $url_parser_php = true;
  4. function replaceBlock ($in, $replace, $start, $end) {
  5. $begin = substr($in,0,$start);
  6. $end = substr($in,$end,strlen($in)-$end);
  7. $ret = $begin.$replace.$end;
  8. return $ret;
  9. }
  10. function parseEmail ($body) {
  11. $body = eregi_replace ("([a-z]|[0-9]|_|\.|-)+\@([a-z]|[0-9]|_|-)+(\.([a-z]|[0-9]|_|-)+)*", "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
  12. return $body;
  13. }
  14. function parseUrl ($body) {
  15. #Possible ways a URL could finish.
  16. $poss_ends=array(" ", "\n", "\r", "<", ">", ".\r", ".\n", ".&nbsp", "&nbsp", ")", "(","&quot");
  17. $done=False;
  18. while (!$done) {
  19. #Look for when a URL starts
  20. $url_tokens = array(
  21. "http://",
  22. "https://",
  23. "ftp://",
  24. "telnet://");
  25. for($i = 0; $i < sizeof($url_tokens); $i++) {
  26. if($where = strpos(strtolower($body), $url_tokens[$i], $start))
  27. break;
  28. }
  29. //$where = strpos(strtolower($body),"http://",$start);
  30. if ($where) {
  31. # Find the end of that URL
  32. reset($poss_ends); $end=0;
  33. while (list($key, $val) = each($poss_ends)) {
  34. $enda = strpos($body,$val,$where);
  35. if ($end == 0) $end = $enda;
  36. if ($enda < $end and $enda != 0) $end = $enda;
  37. }
  38. #Extract URL
  39. $url = substr($body,$where,$end-$where);
  40. #Replace URL with HyperLinked Url
  41. if ($url != "") {
  42. $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
  43. # $body = str_replace($url,$url_str,$body);
  44. $body = replaceBlock($body,$url_str,$where,$end);
  45. $start = strpos($body,"</a>",$where);
  46. } else {
  47. $start = $where + 7;
  48. }
  49. } else {
  50. $done=true;
  51. }
  52. }
  53. return $body;
  54. }
  55. ?>