Clean User Submitted URL's


/ Published in: PHP
Save to your folder(s)

Useful when using user-submitted URLs


Copy this code and paste it in your HTML
  1. # Fix the URL:
  2. $url = preg_replace('/((http:\/\/)?(www\.)?([\w\d-]*?\.)(\w{2,4})/?)/i', 'http://www.$4$5', $d[0]);
  3.  
  4. From:
  5. google.com
  6. www.google.com
  7. google.com/
  8. http://google.com
  9. http://www.google.com
  10. http://www.google.com/
  11.  
  12. To:
  13. http://www.google.com
  14. http://www.google.com
  15. http://www.google.com
  16. http://www.google.com
  17. http://www.google.com
  18. http://www.google.com

URL: http://www.themer.me/blog/scripting/460/clean-url-with-regex

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.