<?php

error_reporting(-1);
mb_internal_encoding('utf-8');
$text1 = "ну что.      не смотрел еще black mesa.я собирался скачать  ,но все как-то некогда было.";
$text2 = "roses are red,and violets are blue.whatever you do i'll keep it for you.";
$text3 = "привет!есть 2 функции,preg_split и explode ,не понимаю,в чем между ними разница.";

/* Делает первую букву в строке заглавной */
function makeFirstLetterUppercase($text) {
	$array = preg_split("/ *[.!?] */", $text, null, PREG_SPLIT_NO_EMPTY);
	foreach($array as $key => $value){
		$firstLetter = mb_strtoupper(mb_substr($value, 0, 1));
		$replace = $firstLetter.mb_substr($value, 1);
		$text = preg_replace("/ *$value */u", $replace, $text);
//		$txt = $txt.$firstLetter.mb_substr($value, 1).". ";
	}
	return $text;
}
/* исправляет текст */
function fixText($text) {
	$array = array(",", "-", ".", "!");
	foreach($array as $k => $rE){
		$text = preg_replace("/ *[$rE] */", "$rE ", $text);
	}
	return $text;
}
$text1 = makeFirstLetterUppercase($text1);
$result = fixText($text1);
echo "{$result}\n";
$text2 = makeFirstLetterUppercase($text2);
$result = fixText($text2);
echo "{$result}\n";
$text3 = makeFirstLetterUppercase($text3);
$result = fixText($text3);
echo "{$result}\n";

