Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post

    trimming and replacing text in a string

    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:

    PHP Code:
    $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

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by markman641 View Post
    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:

    PHP Code:
    $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:
    PHP Code:
    $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 Code:
    <?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
    Last edited by BluePanther; 01-25-2012 at 04:11 AM.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • Users who have thanked BluePanther for this post:

    markman641 (01-25-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •