...

View Full Version : trimming and replacing text in a string



markman641
01-25-2012, 01:59 AM
This seems simple but I cant figure it out.

Anyway, I have a string ($country) and inside that string it says "United States, Canada, France, Germany, United Kingdom"

first off this string will vary. What I am trying to do is replace "United States" with US, Canada with CA, and United Kingdom with UK. Now this can be done with:



$country = "United States, Canada, France, Germany, United Kingdom";
$oldword1 = "United States";
$newword1 = "US";
$oldword2 = "Canada";
$newword2 = "CA";
$oldword3 = "United Kingdom";
$newword3 = "UK";
$text1 = str_replace($oldword1 , $newword1 , $country);
$text2 = str_replace($oldword2 , $newword2 , $text1);
$text3 = str_replace($oldword3 , $newword3 , $text2);
echo $text3


Im sure there is an easier way then that but oh well. ANYWAYS, now I want it to take out the "France" and "germany" OR ANY OTHER COUNTRY except us ca and uk... how do I do it?
Thanks will be given to all that help

BluePanther
01-25-2012, 03:09 AM
This seems simple but I cant figure it out.

Anyway, I have a string ($country) and inside that string it says "United States, Canada, France, Germany, United Kingdom"

first off this string will vary. What I am trying to do is replace "United States" with US, Canada with CA, and United Kingdom with UK. Now this can be done with:



$country = "United States, Canada, France, Germany, United Kingdom";
$oldword1 = "United States";
$newword1 = "US";
$oldword2 = "Canada";
$newword2 = "CA";
$oldword3 = "United Kingdom";
$newword3 = "UK";
$text1 = str_replace($oldword1 , $newword1 , $country);
$text2 = str_replace($oldword2 , $newword2 , $text1);
$text3 = str_replace($oldword3 , $newword3 , $text2);
echo $text3


Im sure there is an easier way then that but oh well. ANYWAYS, now I want it to take out the "France" and "germany" OR ANY OTHER COUNTRY except us ca and uk... how do I do it?
Thanks will be given to all that help

str_replace can take array arguments, so you could just do this:


$strings = array('United States','Canada','United Kingdom');
// Order is important
$replacements = array('US','CA','UK');

$country = str_replace($strings,$replacements,$country);

Now, to exclude the unwanted strings, you could use a regular expression BUT I think it's maybe better to use explode and either array_map (or iterate through the array) and check for unvalid countries. Like this:


<?php
// Your starting string
$string = 'United States, Canada, France, Germany, United Kingdom';
// Array where index is full country name, and value is shortened name
$allowed = array(
'United States' => 'US',
'Canada' => 'CA',
'United Kingdom' => 'UK'
);

// Explode the string into an array
$array = explode(', ',$string);

// For every country, with $key as current array pointer
foreach($array as $key => $country){
// If there's an index in the allowed country list
if(array_key_exists($country,$allowed)){
// Replace that country with the shortened version
$array[$key] = $allowed[$country];
}
else{
// No rule for this country, get rid!
unset($array[$key]);
}
}

// Turn back into a string
$string = implode(', ',$array);
?>

http://codepad.org/8O9rHena - demonstration :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum