How to wrap meta values seperated by comma in <span>?

I was able to display the single value of my custom meta field with comma separated using this code:

$ct = get_post_meta($post->ID, 'ct', true);
echo $ct;

And here’s an output:

USA, UK, China, Hong Kong

I needed the output to be:

<span>USA</span>, <span>UK</span>, <span>China</span>, <span>Hong Kong</span>

Is it possible? If so, a sample code would really help a lot or any reference you might know.

Solutions Collecting From Web of "How to wrap meta values seperated by comma in <span>?"

If you are storing your meta values as a single string separated by commas, you can use the native PHP explode() to store them in an array:

$ct = get_post_meta($post->ID, 'ct', true);
// Store them in an array
$country_array = explode(',' , $ct);
// Run a loop and echo them
foreach( $country_array as $key => $country ){
    echo "<span>{$country}</span>";
    echo ( ( $key < ( count( $country_array ) -1 ) ) ? ', ':'' );

This might cause issue if you have a value like this:

Hong kong, China

Better yet save them as an array, instead of a single string.

Even better is to use custom taxonomy named country, and then use the_terms() to output them. This function allows you to add separator and before & after texts:

the_terms( $post->ID, 'country', '<span>', ',', '</span> );