Search code examples
phpjavascriptwysiwyg

Check the length of typed chars in WYSIWYG


In my site if use textarea + WSYWWYG (Wyzz WYSIWYG).

I would like to minimise and maximise the chars what the user typed. (for ex. min 100 max 1000).

After i post the form, i check the lengt of the posted field. (strln($_POST['text'..) But the WSYWYG editors post the field in HTML formatted so if the user type:fg then the length will be 9 (<P>fg</P>9).

Could someone suggest me anything how can i check the real length of the typed string?

Thank you.


Solution

  • The easiest way, even if it won't give a perfect result, would be to remove the HTML tags from the input, before calling strlen.

    This can be done using the strip_tags function :

    This function tries to return a string with all HTML and PHP tags stripped from a given str .


    So, a portion of code such as this one could help :

    $html = $_POST['text'];
    $stripped = strip_tags($html);
    $length_noHtml = strlen($stripped);
    if ($length_noHtml >= 100 && $length_noHtml <= 1000) {
        // OK
    } else  {
        // Not OK
    }
    


    As a sidenote, if you are using some multi-byte encoding (I'm thinking about UTF-8) for your application, you might want to use the mb_strlen function, instead of strlen :

    • strlen will count the number of bytes in your string
    • while mb_strlen will count the number of characters.