0

I am new to PHP and I have an array and I would like to echo, but formatted so that the country names are written in the proper cases with the first letter capitalized.

For example:

cAnAdA echos as Canada (Cap C)

SwitZerLand echos as Switzerland (Cap S)

I know strtoupper(); all uppercase, strtolower(); all lowercase and ucfirst(); capitalize the first letter, but I want all the cases converted to lowercase and then the first letter capitalized.

Below is my code with the array and foreach loop.

<?php

$countries = [
    'cAnAdA',
    'SwitZerLand',
    'GrEEce',
    'HUnGary',
    'CroATia',
    'IndOneSia',
    'IrElAnd',
    'InDia',
    'MonGoLia',
    'UNitED StaTes of AmeriCA',
    'ChiNa',
    'romaNia',
    'Poland',
    'SieRRA LeoNe',
    'fraNcE',
    'JaPAn',
    'Belgium',
    'TuRkEy',
    'Aland islANds',
    'YeMen',
    'Egypt',
];


foreach($countries as $country){

 echo strtoupper($country);
}

Thank you in advance!!

David Thomas
  • 249,100
  • 51
  • 377
  • 410
Thorburns
  • 19
  • 1

1 Answers1

0
foreach($countries as $country){
    echo ucwords(strtolower($country))
}
aidan byrne
  • 524
  • 1
  • 5
  • 11