Standardize URL String


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

this function processes a URL that 'should' be a full url (i.e. http://something.com/dfsdfs/)
and makes sure it contains http:// for easy inclusion as a link into a href attribute


Copy this code and paste it in your HTML
  1. // this function processes a URL that 'should' be a full url (http://something.com/dfsdfs/)
  2. // and makes sure it contains the correct format to be included into a href attribute
  3. function processURLString($urlString) {
  4. $urlString = trim($urlString);
  5.  
  6. if($urlString) {
  7. $urlString = preg_replace('/https?:\/\//', '', $urlString);
  8. $urlString = trim($urlString);
  9. $urlString = 'http://'.$urlString;
  10. }
  11.  
  12. return $urlString;
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.