It is necessary to take from the variable some arbitrary text written in both small and large letters in an unknown percentage, and if the number of upper case letters in the text is some X percent (a manually specified parameter), then the entire text is translated into lower case.
It is necessary for catching lovers BUILD ADS KAPSLOCK. Verification of the percentage is done for those bona fide users who sometimes allow themselves to single out a few words in caps.

I would like to issue in the form of a function.

  • Well, maybe even for propriety try it yourself? - cyadvert
  • one
    The problem is curious ... If the algorithm: 1. remove everything from the text except letters; 2. count the number of capital; 3. count the percentage of capital of the general text; 4. punish those responsible by changing registar .... Regular expressions are irreplaceable here. I would do that. And how did you try? - cyadvert
  • We translate the text into lower case, compare it with the original through the Levinstein algorithm, look at the number of differing characters, divide by the length of the line — this is the percentage. We are not looking for easy ways) - Mi Ke Bu
  • The task really turned out to be quite interesting, in my opinion, the very place for it is on ruSO. - Mike
  • @Mike, the task, alas, and discharge " do it for me ." - Dmitriy Simushev

1 answer 1

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 Необходимо это для отлова любителей Строчить объявления капслоком. Проверка процентного соотношения делается 
  • Your code does not work for the АБВ line and does not work correctly for the Ёж под Ёлкой line Ёж под Ёлкой . - Dmitriy Simushev
  • one
    @DmitriySimushev Thanks, corrected - Mike