Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Dynamic domain insertion

11:10 pm on Nov 29, 2007 (gmt 0)

New User

10+ Year Member

joined:July 16, 2007
votes: 0


I'm having some trouble figuring out a piece of code for my website. I have very little knowledge about php, so it's difficult for me to figure out. Basically, I want to dynamically insert my domain name, minus the "www" and ".com". I already have a piece of code that gets the page name and dynamically inserts it. Here is the piece of code:

function pageName()
$uri = strtolower(urldecode($_SERVER['REQUEST_URI']));
$uri = substr($uri,0,strpos($uri,"?"));
$uri = strrev(strtolower($uri));
$slash = strpos($uri,"/");
$uri = strrev(substr($uri,0,$slash));
$uri = str_replace('-',' ',$uri);
$uri = str_replace('.html','',$uri);
$uri = str_replace('.htm','',$uri);
$uri = str_replace('.php','',$uri);
$uri = ucwords($uri);


print "".pageName()."";

From what I know, this code will grab the anything after the forward slash, and removes the appened .php, .html, .htm. I think that I would have to change this string: $uri = strrev(substr($uri,0,$slash)), but I don't know what I would change it to.

Here is what I need done:

Example URL (www*widgets*com)

Function should remove: "widgets" from the URL and store it in a variable. I might need to change the Case too...if possible

Any help would be appreciated.


8:24 pm on Dec 1, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 12, 2007
votes: 0

A regular expression version of searching through the full url to find the parts that you want.
You also need to replace the broken vertical bar with the pipe symbol. I dont know why the forum breaks them...its quite annoying.

function pageName()
$dom_patt = '%^(?:http://)?(.*)\.(?:com¦co\.uk¦other_domain)%';
$domain = preg_match($dom_patt, $uri, $dom);
$domain = $dom[1];
$domain = ucwords($domain);
$page_patt = '%/(.*)(?:\.(php¦html?))$%';
$page = preg_match($page_patt, $uri, $pge);
$page = $pge[1];
$page = ucwords($page);
echo 'DOMAIN = '.$domain.'<br />';
echo 'PAGE = '.$page.'<br />';

Iv only included the search for http:// in the $dom_patt encase you want to manually supply the full address to the function. As this is a non capturing pattern it shouldnt slow the regex parser down at all, however if you want the go faster stripes version then remove the (?:http://)?

[edited by: PHP_Chimp at 8:28 pm (utc) on Dec. 1, 2007]

2:31 am on Dec 2, 2007 (gmt 0)

Full Member

10+ Year Member

joined:July 22, 2005
votes: 0

if you just want the domain name

$domainParts = explode('.',$_SERVER['HTTP_HOST']);
$domainName = $domainParts[count($domainParts) - 2];

8:42 pm on Dec 10, 2007 (gmt 0)

New User

10+ Year Member

joined:July 16, 2007
votes: 0

Awesome! Thanks so much.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members