Posted By

jlvallelonga on 08/01/09


Tagged

url format link clean web validate Website address


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

irishsk
umang_nine


formatWebAddress


 / Published in: PHP
 

URL: http://justpushbuttons.com/examples/format_web_address_example.php

this function formats a web address for use in a link. echo formatWebAddress("google.com"); http://www.google.com/

  1. function formatWebAddress($webAddress) {
  2. $originalWebAddress = $webAddress;
  3. $webAddress = "*" . $webAddress;
  4. $formattedWebAddress = "";
  5. if (stripos($webAddress, "http://") == 1) { //starts with http://
  6. if (substr_count($webAddress, ".") == 1) { //one dot only
  7. $formattedWebAddress = $originalWebAddress;
  8. $formattedWebAddress = insertString($formattedWebAddress, "www.", 7);
  9. }
  10. else
  11. $formattedWebAddress = $originalWebAddress;
  12. }
  13. else { //does not start with http://
  14. if (substr_count($webAddress, ".") == 1) //one dot only
  15. $formattedWebAddress = "http://www." . $originalWebAddress;
  16. elseif (substr_count($webAddress, ".") >= 2) //two dots only
  17. $formattedWebAddress = "http://" . $originalWebAddress;
  18. else
  19. $formattedWebAddress = $originalWebAddress;
  20. }
  21. // add slash at end
  22. if (substr_count($formattedWebAddress, "/") == 2) //if it has "//" at beginning
  23. $formattedWebAddress .= "/";
  24. $formattedWebAddress = strtolower($formattedWebAddress);
  25. return $formattedWebAddress;
  26. }

Report this snippet  

You need to login to post a comment.