Code that supports UTF-8 encoding and Russian characters. Counts the length of only letters , spaces, punctuation and other characters are not taken into account. You can take the full length of the string mb_strlen($text) subtract $txtL and $txtU from it and get these non-alphanumeric characters and also calculate their ratio, maybe this can also be useful ...
<?php $text="Необходимо это для отлова любителей СТРОЧИТЬ ОБЪЯВЛЕНИЯ КАПСЛОКОМ. Проверка процентного соотношения делается "; $text=magicLower($text); print $text; function magicLower($text) { $txtL=mb_strlen(preg_replace("/[^а-яёa-z]+/u","",$text)); $txtU=mb_strlen(preg_replace("/[^А-ЯЁA-Z]+/u","",$text)); if(!$txtL) $txtL=0.01; print "LowerCase: $txtL, Upper case: $txtU rate:".($txtU/$txtL)."\n"; if($txtU/$txtL<0.1) return $text; return preg_replace_callback("/(?<=[A-ZА-ЯЁ])([A-ZА-ЯЁ\s]+)/u", function($match) { return mb_convert_case($match[0], MB_CASE_LOWER, "UTF-8"); },$text); } ?>
The magicLower function only converts non-single capital letters to lowercase, possibly separated by a space. Thus, the beauty of normal sentences is preserved; they remain with large letters. The result of this code:
LowerCase: 134, Upper case: 58 rate:0.43283582089552 Необходимо это для отлова любителей Строчить объявления капслоком. Проверка процентного соотношения делается