code_junkie

How do I linkify urls in a string with php

Nov 14th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. "Look on http://www.google.com".
  2.  
  3. $string = "Look on http://www.google.com"
  4. $new_string = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href="\0">\0</a>", $string);
  5.  
  6. $text = preg_replace('@(https?://([-w.]+)+(:d+)?(/([w/_.]*(?S+)?)?)?)@', '<a href="$1">$1</a>', $text);
  7.  
  8. <?php
  9. // The Regular Expression filter
  10. $pattern = "/(http|https|ftp|ftps)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/S*)?/";
  11.  
  12. //example text
  13. $text="Example user text with a URL http://www.zero7web.com , second link http://www.didsburydesign.co.uk";
  14.  
  15. // convert URLs into Links
  16. $text= preg_replace($pattern, "<a href="\0" rel="nofollow">\0</a>", $text);
  17.  
  18. echo $text;
  19. ?>
  20.  
  21. $result = preg_replace('/b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[A-Z0-9+&@#/%=~_|]/i', '<a href=""></a>', $text);
  22.  
  23. <?
  24.  
  25. function link_it($text)
  26. {
  27. $text= preg_replace("/(^|[n ])([w]*?)([w]*?://[w]+[^ ,"nrt<]*)/is", "$1$2<a href="$3" >$3</a>", $text);
  28. $text= preg_replace("/(^|[n ])([w]*?)((www).[^ ,"tnr<]*)/is", "$1$2<a href="http://$3" >$3</a>", $text);
  29. $text= preg_replace("/(^|[n ])([w]*?)((ftp).[^ ,"tnr<]*)/is", "$1$2<a href="ftp://$3" >$3</a>", $text);
  30. $text= preg_replace("/(^|[n ])([a-z0-9&-_.]+?)@([w-]+.([w-.]+)+)/i", "$1<a href="mailto:$2@$3">$2@$3</a>", $text);
  31. return($text);
  32. }
  33.  
  34.  
  35. $text = "ini link gue: http://sapua.com <br>
  36. https://sapua.com <br>
  37. anything1://www.sss.com <br>
  38.  
  39. dua www.google.com <br>
  40. tiga http://www.google.com <br>
  41.  
  42. ftp.sapua.com <br>
  43.  
  44. someone@sapua.com
  45.  
  46.  
  47. ";
  48.  
  49. print link_it($text);
  50.  
  51. ?>
Add Comment
Please, Sign In to add comment