Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What do you mean by 'proper case'? I have a feeling I'm missing something as far as what you exactly need so it'd be nice if you could clarify exactly what this data is and how you want to handle it.</p> <p>You can use <a href="http://www.php.net/ucfirst" rel="nofollow noreferrer"><code>ucfirst</code></a> to make the first letter of each word uppercase, and you can use <a href="http://www.php.net/explode" rel="nofollow noreferrer"><code>explode</code></a> to separate the string into the respective segments:</p> <pre><code>$str = "NEW YORK, NY, US"; list($city, $state, $country) = explode(',', $str); $city = ucfirst(strtolower($city)); </code></pre> <p>In the examples you gave you wouldn't need to do anything to state and country, although there is <a href="http://www.php.net/strtoupper" rel="nofollow noreferrer"><code>strtoupper</code></a> if you want to guarantee they are uppercase.</p> <p>This is a tricky proposition, however, as I am sure there are situations in which whatever output this gives may not be the "correct" way to write a particular city, although I can't think of any examples off the top of my head I am sure there are some.</p> <p>I'm also noticing there are a few lines that just have a country ("US") and some that just have a city ("HONG KONG") - there's no reliable way to determine what the string contains. You could try matching it with a list of countries and cities and so forth, but it seems like whatever solution you come up with it's going to be a guessing game at best.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload