fork(5) download
  1. <?
  2. $template = <<< EOF
  3. Hello. Have you visited google.comm today?
  4. Hello. Have you visited www.google.com today?
  5. Hello. Have you visited http://google.com today?
  6. Hello. Have you visited https://google.com today?
  7. EOF;
  8.  
  9. $template = preg_replace_callback('/(?=(([\w\/\/:.]+)\.(?:com|net|org|info|no|dk|se)))\b(?:(?:https?|ftp|file):\/\/|(?:www\.|ftp\.)?)
  10. (?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*
  11. (?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/ix','my_callback',$template);
  12.  
  13. function my_callback($matches) {
  14.  
  15. if (preg_match('/https?/ix', $matches[1])) {
  16. $link = $matches[1];
  17. return "<a href=\"$link\">$link</a>";
  18. } else {
  19. $link = $matches[1];
  20. return "<a href=\"http://$link\">http://$link</a>";
  21. }
  22. }
  23.  
  24. echo $template;
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Hello. Have you visited <a href="http://google.com">http://google.com</a> today?
Hello. Have you visited <a href="http://www.google.com">http://www.google.com</a> today?
Hello. Have you visited <a href="http://google.com">http://google.com</a> today?
Hello. Have you visited <a href="https://google.com">https://google.com</a> today?