note 42086 added to function.substr

From: Date: Mon, 03 May 2004 09:09:20 +0000
Subject: note 42086 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-69127@lists.php.net to get a copy of this message
I wanted to "clean up" a URL to get domain and sub-domain only. This is what I ended up with: <?php //This is the example url we want to play with $url = 'http://www.somewhere.com/somefile.htm'; print("Before: $url<br>\n"); //Make a function for this simple operation function urltodomain($url) { //Check if the string actually contains "http://" if(strstr($url, 'http://')) { // "http://" = 7 characters $url = substr($url, 7); //Reverse strstr to omit "/somefile.htm..." return substr($url, 0, strlen($url) - strlen(strstr($url, '/'))); } else { //Could not find "http://" in the url, return the url as it is instead return $url; } } //See if it works print('After: ' . urltodomain($url) . "<br>\n"); //This should return: //Before: http://www.somewhere.com/somefile.htm<br> //After: www.somewhere.com<br> ?> This function is simple, but not very flexible. The URL could be from some other protocol (https:// for instance), so a elseif{} could be added in the function for each protocol (or some way to check how many characters the first substr() should eat). ---- Manual Page -- http://www.php.net/manual/en/function.substr.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42086 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42086&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42086&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#69127) next »