StringURLLinker.inc 630 B

1234567891011121314151617181920
  1. <?php
  2. class StringURLLinker {
  3. public function replaceUrlsWithLinks($string) {
  4. $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
  5. if(preg_match($reg_exUrl, $string, $url)) {
  6. return preg_replace($reg_exUrl, '<a href="' . $url[0] . '">'.$url[0].'</a>', $string);
  7. } else {
  8. return $string;
  9. }
  10. }
  11. public function stringOnlyContainsUrl($string) {
  12. $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
  13. if(preg_match($reg_exUrl, $string, $url)) {
  14. //string contains a url. Is it the only content?
  15. return $string == $url[0];
  16. }
  17. return false;
  18. }
  19. }