Posted By


jlvallelonga on 08/01/09

Tagged


Statistics


Viewed 719 times
Favorited by 1 user(s)

formatWebAddress


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

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


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.