Welcome to WebmasterWorld Guest from 54.227.68.206

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Parse Domain Name

     
8:31 pm on Nov 30, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 12, 2007
posts:74
votes: 0


Hello Everyone:

I am trying to parse a domain name. It's better if I give an example of what I am trying to accomplish.

Example domain name:
http://www.alabamawidgetsonline.com


What I want to do is:

1.)extract the domain without the
http://www.
and also without the .com
2.)Manipulate the domain name to have spaces between each word...alabama widgets online
3.)Capitalize each word.

Another thing I would like to do is to extract just the state name...Alabama.

Thanks in advance.
8:35 pm on Nov 30, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member topr8 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2002
posts:3210
votes: 13


are the domains always of a certain format?
using known words, such as the state.
8:55 pm on Nov 30, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 12, 2007
posts:74
votes: 0


yes, they would all be state name widgets online.

I tried doing the following:

<?php

$darray = explode('.', $_SERVER['HTTP_HOST']);
$narray = array_reverse($darray);
$domainTitle = $narray[1];
unset($darray, $narray);
ucfirst($domainTitle);
// The word we want to replace
$oldWord = "widgetsonline";
// The new word we want in place of the old one
$newWord = "Widgets Online";
// Run through the text and replaces all occurrences of $oldText
$domainTitle = str_replace($oldWord , $newWord , $domainTitle);

?>

That's as far as I got though.
10:05 pm on Nov 30, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


You've pretty much got it! Just a couple of tweaks...

ucfirst($domainTitle);


Should be:
$domainTitle = ucfirst($domainTitle);


Since ucfirst() does work in-place, it just returns the altered value.

$newWord = "Widgets Online";


Add a space:
$newWord = " Widgets Online";


Then echo $domainTitle.
2:24 pm on Dec 1, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 12, 2007
posts:74
votes: 0


That works awesome! Thank you so much. My boss will be really happy.

The last step would be to extract the state name. Is there a way to do that with regular expressions, let's say.

If I know that the rest of the domain is going to remain constant, such as my example above:

(state)widgetsonline.com

Could I use regular expressions to say something like grab text left of widgetsonline?

or is there another, easier solution, adding to the code that works above?

Thanks in advance and thanks to penders for the quick and easy solution.
4:28 pm on Dec 1, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


To extract the state name you might as well just add a bit of code to what you already have...
// Run through the text and replaces all occurrences of $oldText 
$domainTitle = str_replace($oldWord , $newWord , $domainTitle);


Insert the one line...
// Run through the text and replaces all occurrences of $oldText
$stateName = str_replace($oldWord, '', $domainTitle);
$domainTitle = str_replace($oldWord, $newWord, $domainTitle);



Correction to my post above...
Since ucfirst() does work in-place, ...

Should read...
Since ucfirst() does not work in-place, ...

!
(Meaning that it does not change the actual value of the variable you pass to it.)
6:09 pm on Dec 1, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 12, 2007
posts:74
votes: 0


That works perfectly. Thank you so much for the help penders.
Hopefully this helps other in future if they need this kind of functionality.