我的答案部分基于@Carlos 的答案,但在这里和那里进行了一些调整,并包装成一个可重用的函数。它应该有能力完成工作,对吧!
/* A global function to sort a [STRING] comma-delimited list both alphabetically, and numerically, and then return the sorted [STRING] list back to the function caller. [BEGIN] */
if (!function_exists('sortCommaDelimitedListStr'))
{
function sortCommaDelimitedListStr($var)
{
$arr = array_filter(array_map('trim', explode(',', $var)));
sort($arr);
$arr = implode(', ', $arr);
return $arr;
};
};
/* A global function to sort a [STRING] comma-delimited list both alphabetically, and numerically, and then return the sorted [STRING] list back to the function caller. [END] */
要使用,只需像这样调用函数:
$xyz = "Europe, France, Italy, Spain, UK, US,Nordic, West Europe, Belgium, Luxembourg, Netherlands, Sweden,US,Asia, Europe, Israel, North America, India,North America, , China, Hong Kong,West North Central, West South Central,UK,East South Central,Middle Atlantic, Greater China, Malaysia, Singapore, Taiwan, Middle Atlantic, Global, Australasia, Central and East Europe";
$xyz = sortCommaDelimitedListStr($xyz);