homepage Welcome to WebmasterWorld Guest from
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

Extracting Characters From String

 6:25 pm on Dec 1, 2011 (gmt 0)

Hello Everyone:

I am working through a situation utilizing php to perform a task. I have been reading a php book and it gives examples of doing separate things, but I think I need to combine a few of them to accomplish my goal. That's where I get confused.

My situation: I have 50 websites with the state name in the domain. For example:

*Note #1: they will all use the same naming style, (statename)widgetsonline.com

*Notes #2: I will not be using a mysql database.

Mission: To extract the state from the domain, run it through some type of php command to look for the state name and to output the associated abbreviation for the state.

In my studying, it gives an example of doing multidimensional arrays and I am guessing here, but my thoughts on applying it for my mission are:

$States = array (
array (
'state' => 'alabama',
'abbrev' => 'AL',
array (
'state' => 'alaska',
'abbrev' => 'AK',
and so on down the line

Then, my thinking was that I somehow get the state name extracted from the domain, put that in a variable, run it through a loop, and once it finds the state, to have an if then statement or a for each statement that gets the abbreviation and then to put that in another variable so I may use it on my web page, then breakout of loop once the condition is met, so it doesn't run a continuous loop.

I might be making this very difficult for myself, but I am green when it comes to using php.

I'd appreciate any insight on this and welcome suggestions.

Thanks in advance,




 6:41 pm on Dec 1, 2011 (gmt 0)

construct your array like this:

$state_abbr = array(
[... and so on]

Then get the state abbreviation simply:

$abbreviation = $state_abbr['alabama']

associative arrays are key=>value pairs. So if you know the key, you retrieve the value.
There's no need for looping or breaking.


 6:43 pm on Dec 1, 2011 (gmt 0)

Extracting the state would be more difficult than, say, comparing the first x chars of the domain name to the state name. The logic something like this, for example:

$states = all the states with match codes
$domains = array of all domains

foreach $domains as $domain {
foreach $states as $state {
$len = length of $state string
if(substr($domain, 0, $len) == $state) {
// we have a match, return abbrev.

Now see if you can't come up with the actual code yourself. :)


 6:59 pm on Dec 1, 2011 (gmt 0)

no no no. the string chopping is the easy part.

$domain = "alabamawidgetsonline.com";

$state = substr($domain, 0, (strlen("widgetsonline.com") * -1));

or, more simply,

// because 17 is the length of "widgetsonline.com"
$state = substr($domain, 0, -17);

// $state == "alabama"

substr() will grab from a position relative to the end of the string, if the third argument is a negative integer.


again, no loops, no conditions.


 7:02 pm on Dec 1, 2011 (gmt 0)

however, if you're not 100% sure that the domain will ALWAYS be constructed like that, then you should create a lookup array. Since you're looking up an abbreviation, you might as well.

$domain_map = array(
"alabamawidgetsonline.com" => "AL",
"alaskawidgetsonline.com" => "AK",

Do it all in one step.

$state_abbr = $domain_map[$domain];



 7:37 pm on Dec 1, 2011 (gmt 0)

I love your solutions. It seems so easy when guided. One more thing I will add to the mix that I found online is doing a switch. Then, applying code performed in this forum earlier gives us the code below. It works, except for one thing, states with two words. I need to find a way to make states like North Dakota written as two words. The code below would output it as Southdakota.

*Note: This code performs other things not pertaining to this topic, but a previous topic I was asking about.

$darray = explode('.', $_SERVER['HTTP_HOST']);
$narray = array_reverse($darray);
$domainTitle = $narray;
unset($darray, $narray);
$domainTitle = 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
$stateName = str_replace($oldWord, '', $domainTitle);
$domainTitle = str_replace($oldWord , $newWord , $domainTitle);

switch( $stateName ) {

case 'Alaska':

$abbr = 'AK';


case 'Alabama':

$abbr = 'AL';




[1][edited by: eelixduppy at 8:45 pm (utc) on Dec 1, 2011]
[edit reason] snipped for brevity [/edit]


 8:52 pm on Dec 1, 2011 (gmt 0)

"alabamawidgetsonline.com" => "AL",

Now that's not very scalable. ;)


This seems very verbose and unnecessary. I would at least change this part to how httpwebwitch suggested, with a
"state name" => "abbv" mapping in an array.

 8:56 pm on Dec 1, 2011 (gmt 0)

$data = array(
"alabamawidgetsonline.com" => array(
"abbr" => "AL",
"name" => "Alabama"
"northdakotawidgetsonline.com" => array(
"abbr" => "ND",
"name" => "North Dakota"

put that in a separate file. call it "state_data.php";

use include() or require() to include it at the top of your script. Tuck it away.

Then do this:

// $domain = "northdakotawidgetsonline.com";

$state_data = $data[$domain];

$state_name = $state_data['name'];


 5:26 pm on Dec 7, 2011 (gmt 0)


This is a bit belated, but thank you so much for the help with this. It works perfectly and is fast. I am surprised how fast php can run through all the information and output the results.

Your solution is a great way to organize a lot of different information.

Thanks again,



 6:10 pm on Dec 7, 2011 (gmt 0)

Thanks, I'm always glad to help.

multidimensional arrays (that's what those are called) are great for storing dictionary-like lookup tables for data that doesn't change, that you want to grab quickly.

They are fast... to a point. Good for small-ish data sets when you don't mind hard-coding them in a file. A few hundred items or less, that's fine. When you get beyond that, with lists that are 300, 400, 1000... it's time to look at using SQL or memcached or APC or SQLite or something else.

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