Make every first character uppercase in array

Viewed 26546

I'm trying to get all my first characters in a PHP array to be uppercase.

PHP code:

<?php
$ordlista = file_get_contents('C:/wamp/www/bilder/filmlista.txt');

$ord = explode("\n", $ordlista);

sort($ord,SORT_STRING);

foreach ($ord as $key => $val) {
    echo $val."<br/>";
}
?>
4 Answers

Sometimes, the raw data would looks something like this:

$ord = ['apple', 'GUAVA', 'mango', 'BANANA'];

To full-proof this:

$ord = array_map('ucfirst', array_map('strtolower', $ord));

What this does is it converts everything into a lower case first, then capitalize each word.

Related