Search code examples
phpentitiesstrlen

Count textarea signs correct php mb_strlen


function countCharacterNoCK($contentVar, $maximumChars){

        $countThisString = strip_tags($contentVar);

        $umlaute = Array("/ä/","/ö/","/ü/","/Ä/","/Ö/","/Ü/","/ß/");
        $replace = Array("a","o","u","A","O","U","s");
        $countThisString1 = preg_replace($umlaute, $replace, $countThisString);

        $lenght = mb_strlen(html_entity_decode($countThisString1, ENT_QUOTES, 'UTF-8'));

        if($lenght < $maximumChars+1){
            return TRUE;
        }else{
            return $lenght;
        }
}

This function doesn´t work as expected. Problem is: I´ve got a <textarea>, in which i can Press Enter.

<textarea name="testvalue">
12345 1234
1234512345
</textarea>

if i call my function $test = countCharacterNoCK($_POST['testvalue'], 20); it´ll return "22".

How to solve this? I´ve tried it with strip_tags but it didn´t solve my problem...I want the function to count like MS Office (Word), but can´t get this solved...any tipps?


Solution

  • i guess that it is counting the invisible character of new lines '\n'.

    you should try trim first that instead

    $temporarystring =trim($countThisString1);
    $lenght = mb_strlen(html_entity_decode($temporarystring, ENT_QUOTES, 'UTF-8'));