fork download
  1. <?php
  2.  
  3. function replace_callback($m){
  4. if (empty($m[3])) { // email
  5. return "<a href='mailto:".$m[0]."'>" . $m[0] . "</a>";
  6. }
  7. else { // url
  8. return "<a href='".$m[1]."://".$m[3]."' target='_blank' title='".$m[0]."'>".$m[0]."</a>";
  9. }
  10. }
  11.  
  12. function linkifyMyString($noteText) {
  13. $emailPattern = '\S+@\S+\.\S+';
  14. $urlPattern = '(https?)?(://)?([a-zA-Z](?:[-\w]+\.)+(?:[^\s.]+\S*)+[^,.\s])';
  15. return preg_replace_callback('~' . $emailPattern . '|' . $urlPattern . '~', 'replace_callback', $noteText);
  16. }
  17. $str = "www.google.com contact me at test.me@gmail.com visit us http://google.com ,http://g...content-available-to-author-only...l.com";
  18. echo linkifyMyString($str);
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
<a href='://www.google.com' target='_blank' title='www.google.com'>www.google.com</a> contact me at <a href='mailto:test.me@gmail.com'>test.me@gmail.com</a> visit us <a href='http://google.com' target='_blank' title='http://google.com'>http://google.com</a> ,<a href='http://g...content-available-to-author-only...l.com' target='_blank' title='http://g...content-available-to-author-only...l.com'>http://g...content-available-to-author-only...l.com</a>