Posted By

Entherman on 08/21/11


Tagged

regex regular Expression


Versions (?)

Clean User Submitted URL's


 / Published in: PHP
 

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

Useful when using user-submitted URLs

  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

Report this snippet  

You need to login to post a comment.