Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP Challenge Reply To: PHP Challenge

#184789
_monty
Participant

Try this…

<?php
$file_contents = trim(file_get_contents(‘/full/path/to/file.txt’));

if($file_contents)
{
$sum = 0;
$alpha_count_arr = array();
$contents_to_arr = array_map(‘trim’,array_filter(explode(“\n”, $file_contents)));

foreach ($contents_to_arr as $key => $value)
{

if(is_numeric($value))
{
$sum += $value;
}
elseif(ctype_alpha($value))
{
if(!isset($alpha_count_arr[$value]))
{
$alpha_count_arr[$value] = 1;
}
else
{
$alpha_count_arr[$value]++;
}
}

}

if(count($alpha_count_arr) > 0)
{
ksort($alpha_count_arr);
foreach ($alpha_count_arr as $character => $count)
{
echo “$character = $count<br/>”;
}
}

echo “<br/>Numeric value total: “.$sum;

}
?>