fork(2) download
  1. <?php
  2.  
  3. function linkify($input){
  4. $re = <<<'REGEX'
  5. !
  6.   (
  7.   <\w++
  8.   (?:
  9.   \s++
  10.   | [^"'<>]++
  11.   | "[^"]*+"
  12.   | '[^']*+'
  13.   )*+
  14.   >
  15.   )
  16.   |
  17.   (\b https?://[^\s"'<>]++ )
  18.   |
  19.   (\b www\d*+\.\w++[^\s"'<>]++ )
  20. !xi
  21. REGEX;
  22.  
  23. return preg_replace_callback($re, function($m){
  24. if($m[1]) return $m[1];
  25. $url = htmlspecialchars($m[2] ? $m[2] : "http://$m[3]");
  26. $text = htmlspecialchars("$m[2]$m[3]");
  27. return "<a rel='nofollow' href='$url'>$text</a>";
  28. },
  29. $input);
  30. }
  31.  
  32. echo linkify("<img src='http://foo'>www.test.com/?x&y");
  33.  
  34. ?>
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
<img src='http://foo'><a rel='nofollow' href='http://w...content-available-to-author-only...t.com/?x&amp;y'>www.test.com/?x&amp;y</a>