Forum Moderators: coopster
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']));
if(strpos($uri,"?")>0)
$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);
return($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.
Thanks
<?php
function pageName()
{
$uri = $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$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 />';
}
pageName();
?>
<edit>
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]