使用PHP如何将此字符串变量转换为另一种形式?

时间:2014-11-27 10:21:14

标签: php

我有一个很大的city, country列表,我希望以紧凑的形式显示它们。

例如,我有以下变量

Hallands, Sweden

但是,在, and space保留两个字母并大写之后,我怎么能像这里一样显示呢。

Hallands, SW ?

8 个答案:

答案 0 :(得分:1)

示例:

$var = explode($variable, ", ");
echo $var[0].", ".strtoupper(substr($var[1], 0, 2));

substr将得到前两个字母,strtoupper将把它们大写。

编辑:我看到你将它们放在一个变量中。我可能会首先爆炸变量,然后应用上面的方法。

答案 1 :(得分:1)

然后试试这个

$str='Hallands, Sweden';

$str=explode(',',str);
 echo $str[0].','.strtoupper(substr($str[1], 0, 2));

答案 2 :(得分:0)

这样的事情:

$str = "Hallands, Sweden";
$strs = explode(',', $str);

$str_country = strtoupper(substr($strs[1], 1,2));

$str = $strs[0] . ', ' . $str_country;

print_r($str);

答案 3 :(得分:0)

这应该这样做。 'trim'用于删除单词两侧的空格。

$str = "Hallands, Sweden";
$modified = array_map('trim', explode(',', $str));
$modified = $modified[0].', '.strtoupper(substr($modified[1], 0, 2));

echo $modified;

答案 4 :(得分:0)

以下是所有步骤:

  • 将字符串拆分为城市和国家/地区
  • 将国家/地区缩减为两个字符
  • 大写其余两个字符
  • 将它们粘贴在一起

在代码中,按顺序:

// splitting
$parts = explode( ", ", $string ); // split over the comma and space
$city = $parts[0]; // city is first
$country = $parts[1]; // country is second

// shorten and uppercase
$short_country = substr( $country, 0, 2 ); // shorten to 2 chars
$short_country_uppercase = strtoupper( $short_country ); // uppercase the 2 chars

// paste back together
$new_string = $city . ", " . $short_country_uppercase;

这是不必要的长期,主要是因为它可以帮助您了解正在发生的事情。如果你愿意,你可以像其他答案一样将它缩短到一行,但是知道代码实际上做了什么有助于你学习。

答案 5 :(得分:0)

你说有一个巨大的列表,这段代码可以帮助你

<?php

$variable_seperated_with_comma="Newyork,canada,ohio,indiana";
$countries=explode(',',$fullvariable_seperated_with_comma);
 foreach($countries as $country){
    echo strtoupper(substr($country, 0, 2));
    echo '</br>';
 }

&GT;

答案 6 :(得分:0)

正如评论中所提到的,人们将比ISO 3166-1 country codes更熟悉国家/地区名称的子字符串。

以下是ISO国家/地区代码的要点:https://gist.github.com/vxnick/380904

<?php

$string = 'Hallands, Sweden';
$ex = explode(",", $string); //Split the string
$countries_flip = array_flip( array_map('strtolower', getCountryCodeMap())); //Swap value and keys around (so we dont have to use array_search)

echo $ex[0] . ', ' . $countries_flip[trim(strtolower($ex[1]))]; //Output result

function getCountryCodeMap() {
//https://gist.github.com/vxnick/380904
    return array
        (
        'AF' => 'Afghanistan',
        'AX' => 'Aland Islands',
        'AL' => 'Albania',
        'DZ' => 'Algeria',
        'AS' => 'American Samoa',
        'AD' => 'Andorra',
        'AO' => 'Angola',
        'AI' => 'Anguilla',
        'AQ' => 'Antarctica',
        'AG' => 'Antigua And Barbuda',
        'AR' => 'Argentina',
        'AM' => 'Armenia',
        'AW' => 'Aruba',
        'AU' => 'Australia',
        'AT' => 'Austria',
        'AZ' => 'Azerbaijan',
        'BS' => 'Bahamas',
        'BH' => 'Bahrain',
        'BD' => 'Bangladesh',
        'BB' => 'Barbados',
        'BY' => 'Belarus',
        'BE' => 'Belgium',
        'BZ' => 'Belize',
        'BJ' => 'Benin',
        'BM' => 'Bermuda',
        'BT' => 'Bhutan',
        'BO' => 'Bolivia',
        'BA' => 'Bosnia And Herzegovina',
        'BW' => 'Botswana',
        'BV' => 'Bouvet Island',
        'BR' => 'Brazil',
        'IO' => 'British Indian Ocean Territory',
        'BN' => 'Brunei Darussalam',
        'BG' => 'Bulgaria',
        'BF' => 'Burkina Faso',
        'BI' => 'Burundi',
        'KH' => 'Cambodia',
        'CM' => 'Cameroon',
        'CA' => 'Canada',
        'CV' => 'Cape Verde',
        'KY' => 'Cayman Islands',
        'CF' => 'Central African Republic',
        'TD' => 'Chad',
        'CL' => 'Chile',
        'CN' => 'China',
        'CX' => 'Christmas Island',
        'CC' => 'Cocos (Keeling) Islands',
        'CO' => 'Colombia',
        'KM' => 'Comoros',
        'CG' => 'Congo',
        'CD' => 'Congo, Democratic Republic',
        'CK' => 'Cook Islands',
        'CR' => 'Costa Rica',
        'CI' => 'Cote D\'Ivoire',
        'HR' => 'Croatia',
        'CU' => 'Cuba',
        'CY' => 'Cyprus',
        'CZ' => 'Czech Republic',
        'DK' => 'Denmark',
        'DJ' => 'Djibouti',
        'DM' => 'Dominica',
        'DO' => 'Dominican Republic',
        'EC' => 'Ecuador',
        'EG' => 'Egypt',
        'SV' => 'El Salvador',
        'GQ' => 'Equatorial Guinea',
        'ER' => 'Eritrea',
        'EE' => 'Estonia',
        'ET' => 'Ethiopia',
        'FK' => 'Falkland Islands (Malvinas)',
        'FO' => 'Faroe Islands',
        'FJ' => 'Fiji',
        'FI' => 'Finland',
        'FR' => 'France',
        'GF' => 'French Guiana',
        'PF' => 'French Polynesia',
        'TF' => 'French Southern Territories',
        'GA' => 'Gabon',
        'GM' => 'Gambia',
        'GE' => 'Georgia',
        'DE' => 'Germany',
        'GH' => 'Ghana',
        'GI' => 'Gibraltar',
        'GR' => 'Greece',
        'GL' => 'Greenland',
        'GD' => 'Grenada',
        'GP' => 'Guadeloupe',
        'GU' => 'Guam',
        'GT' => 'Guatemala',
        'GG' => 'Guernsey',
        'GN' => 'Guinea',
        'GW' => 'Guinea-Bissau',
        'GY' => 'Guyana',
        'HT' => 'Haiti',
        'HM' => 'Heard Island & Mcdonald Islands',
        'VA' => 'Holy See (Vatican City State)',
        'HN' => 'Honduras',
        'HK' => 'Hong Kong',
        'HU' => 'Hungary',
        'IS' => 'Iceland',
        'IN' => 'India',
        'ID' => 'Indonesia',
        'IR' => 'Iran, Islamic Republic Of',
        'IQ' => 'Iraq',
        'IE' => 'Ireland',
        'IM' => 'Isle Of Man',
        'IL' => 'Israel',
        'IT' => 'Italy',
        'JM' => 'Jamaica',
        'JP' => 'Japan',
        'JE' => 'Jersey',
        'JO' => 'Jordan',
        'KZ' => 'Kazakhstan',
        'KE' => 'Kenya',
        'KI' => 'Kiribati',
        'KR' => 'Korea',
        'KW' => 'Kuwait',
        'KG' => 'Kyrgyzstan',
        'LA' => 'Lao People\'s Democratic Republic',
        'LV' => 'Latvia',
        'LB' => 'Lebanon',
        'LS' => 'Lesotho',
        'LR' => 'Liberia',
        'LY' => 'Libyan Arab Jamahiriya',
        'LI' => 'Liechtenstein',
        'LT' => 'Lithuania',
        'LU' => 'Luxembourg',
        'MO' => 'Macao',
        'MK' => 'Macedonia',
        'MG' => 'Madagascar',
        'MW' => 'Malawi',
        'MY' => 'Malaysia',
        'MV' => 'Maldives',
        'ML' => 'Mali',
        'MT' => 'Malta',
        'MH' => 'Marshall Islands',
        'MQ' => 'Martinique',
        'MR' => 'Mauritania',
        'MU' => 'Mauritius',
        'YT' => 'Mayotte',
        'MX' => 'Mexico',
        'FM' => 'Micronesia, Federated States Of',
        'MD' => 'Moldova',
        'MC' => 'Monaco',
        'MN' => 'Mongolia',
        'ME' => 'Montenegro',
        'MS' => 'Montserrat',
        'MA' => 'Morocco',
        'MZ' => 'Mozambique',
        'MM' => 'Myanmar',
        'NA' => 'Namibia',
        'NR' => 'Nauru',
        'NP' => 'Nepal',
        'NL' => 'Netherlands',
        'AN' => 'Netherlands Antilles',
        'NC' => 'New Caledonia',
        'NZ' => 'New Zealand',
        'NI' => 'Nicaragua',
        'NE' => 'Niger',
        'NG' => 'Nigeria',
        'NU' => 'Niue',
        'NF' => 'Norfolk Island',
        'MP' => 'Northern Mariana Islands',
        'NO' => 'Norway',
        'OM' => 'Oman',
        'PK' => 'Pakistan',
        'PW' => 'Palau',
        'PS' => 'Palestinian Territory, Occupied',
        'PA' => 'Panama',
        'PG' => 'Papua New Guinea',
        'PY' => 'Paraguay',
        'PE' => 'Peru',
        'PH' => 'Philippines',
        'PN' => 'Pitcairn',
        'PL' => 'Poland',
        'PT' => 'Portugal',
        'PR' => 'Puerto Rico',
        'QA' => 'Qatar',
        'RE' => 'Reunion',
        'RO' => 'Romania',
        'RU' => 'Russian Federation',
        'RW' => 'Rwanda',
        'BL' => 'Saint Barthelemy',
        'SH' => 'Saint Helena',
        'KN' => 'Saint Kitts And Nevis',
        'LC' => 'Saint Lucia',
        'MF' => 'Saint Martin',
        'PM' => 'Saint Pierre And Miquelon',
        'VC' => 'Saint Vincent And Grenadines',
        'WS' => 'Samoa',
        'SM' => 'San Marino',
        'ST' => 'Sao Tome And Principe',
        'SA' => 'Saudi Arabia',
        'SN' => 'Senegal',
        'RS' => 'Serbia',
        'SC' => 'Seychelles',
        'SL' => 'Sierra Leone',
        'SG' => 'Singapore',
        'SK' => 'Slovakia',
        'SI' => 'Slovenia',
        'SB' => 'Solomon Islands',
        'SO' => 'Somalia',
        'ZA' => 'South Africa',
        'GS' => 'South Georgia And Sandwich Isl.',
        'ES' => 'Spain',
        'LK' => 'Sri Lanka',
        'SD' => 'Sudan',
        'SR' => 'Suriname',
        'SJ' => 'Svalbard And Jan Mayen',
        'SZ' => 'Swaziland',
        'SE' => 'Sweden',
        'CH' => 'Switzerland',
        'SY' => 'Syrian Arab Republic',
        'TW' => 'Taiwan',
        'TJ' => 'Tajikistan',
        'TZ' => 'Tanzania',
        'TH' => 'Thailand',
        'TL' => 'Timor-Leste',
        'TG' => 'Togo',
        'TK' => 'Tokelau',
        'TO' => 'Tonga',
        'TT' => 'Trinidad And Tobago',
        'TN' => 'Tunisia',
        'TR' => 'Turkey',
        'TM' => 'Turkmenistan',
        'TC' => 'Turks And Caicos Islands',
        'TV' => 'Tuvalu',
        'UG' => 'Uganda',
        'UA' => 'Ukraine',
        'AE' => 'United Arab Emirates',
        'GB' => 'United Kingdom',
        'US' => 'United States',
        'UM' => 'United States Outlying Islands',
        'UY' => 'Uruguay',
        'UZ' => 'Uzbekistan',
        'VU' => 'Vanuatu',
        'VE' => 'Venezuela',
        'VN' => 'Viet Nam',
        'VG' => 'Virgin Islands, British',
        'VI' => 'Virgin Islands, U.S.',
        'WF' => 'Wallis And Futuna',
        'EH' => 'Western Sahara',
        'YE' => 'Yemen',
        'ZM' => 'Zambia',
        'ZW' => 'Zimbabwe',
    );
}
?>

答案 7 :(得分:0)

以下是我的表现:

// input string
$str = 'Hallands, Sweden';

// list the countries and their country codes
// i'd strtolower/strtoupper them all to make them case-insensitive, but i'll leave that up to you
$countries = array(
    // ...
    'Sweden' => 'SE'
    // ...
);

// separate $str at the comma
$parts = explode(',', $str);

// set the vars to false to later check if they were set correctly
$county = $parts[0]; // Hallands
$country = false;
$countrycode = false;

// check that you at least have two parts
if (count($parts) > 1) {
    $country = trim($parts[1]); // Sweden
    $countrycode = $country; // failsafe

    // check if the country is in the list
    if (array_key_exists($country, $countries)) {
        $countrycode = $countries[$country]; // SE
    }
}

// check if the vars are set correctly
if ($country != false && $countrycode != false) {
    // use them
    $str = $county.', '.$countrycode; // Hallands, SE
} else {
    // something went wrong
}