homepage Welcome to WebmasterWorld Guest from 54.145.183.126
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Parse Domain Name
mkingsle

5+ Year Member



 
Msg#: 4392662 posted 8:31 pm on Nov 30, 2011 (gmt 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.

 

topr8

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



 
Msg#: 4392662 posted 8:35 pm on Nov 30, 2011 (gmt 0)

are the domains always of a certain format?
using known words, such as the state.

mkingsle

5+ Year Member



 
Msg#: 4392662 posted 8:55 pm on Nov 30, 2011 (gmt 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.

penders

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



 
Msg#: 4392662 posted 10:05 pm on Nov 30, 2011 (gmt 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.

mkingsle

5+ Year Member



 
Msg#: 4392662 posted 2:24 pm on Dec 1, 2011 (gmt 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.

penders

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



 
Msg#: 4392662 posted 4:28 pm on Dec 1, 2011 (gmt 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.)

mkingsle

5+ Year Member



 
Msg#: 4392662 posted 6:09 pm on Dec 1, 2011 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved