willoller.com

Avatar

Learning to match the beat of the Old World man.

Counting Characters php vs javascript

On a recent project, an issue cropped up where we were exporting xml reports to a third party, who had rules about line lengths in Description fields.

So, I wrote up a quick-and-dirty character counter for the description field so the client can keep their descriptions under the 1500-word limit.

Here’s how it was done:


if (!$fdata['description']) {
   $errors[] = "You must enter a Description";
} elseif(strlen($fdata['description']) > 1500) {
   $len = strlen($fdata['description']);
   $errors[] = "Descriptions are limited to 1500 characters ($len)";
}

 

Now the html and javascript:


<li class="description">
   <label for="htmlarea" class="required">Description *</label>
   <textarea name="description" id="htmlarea" cols="80" rows="20" class="textarea">< ?= $ff['description'] ?></textarea>
   <p class="hint" id="desc_char_count">1500 character limit</p>
</li>
 

$(function(){
   $('#htmlarea').keyup(function(){
      var charLength = $(this).val().length;
      $('#desc_char_count').html(charLength + ' of 1500 characters used');
      if($(this).val().length > 1500) {
         $('#desc_char_count').removeClass('positive').addClass('negative').html('<strong>You have ' + $(this).val().length + ' characters. You may only have up to 1500 characters.</strong>');
      } else {
         $('#desc_char_count').addClass('positive').removeClass('negative');
      }
   });
   $('#htmlarea').keyup();
});

 

All was working well, except I underestimated the descriptions. The next morning, the thing was broken. The php side was kicking validation errors (“over 1500 characters”), while the javascript was counting a mere 1480. Why?

Turns out the data was full of bullets and other multibyte characters. To make the counts match, I needed multibyte-safe php:


if (!$fdata['description']) {
   $errors[] = "You must enter a Description";
} elseif(mb_strlen($fdata['description'],'UTF-8') > 1500) {
   $len = mb_strlen($fdata['description'],'UTF-8');
   $errors[] = "Descriptions are limited to 1500 characters ($len)";
}

 

Hooray! Now the counts match!

No Comments, Comment or Ping

Reply to “Counting Characters php vs javascript”

Before you go