I have an textarea in my form and I want to show how many characters are entered. I have put the code in a test script below.

I had a problem because the js count was not matching strlen().

I think that I have found the problem:

On my keyboard I also have Turkish characters so when I enter a ğ
( on my screen this shows as a g with a "hat" on top)
the js counter shows just one character - but when I submit the form the
strlen() returns 6.

So whenever I enter a few turkish characters - the count jumps.

My problem is that my web page has the javascript counter on it but the checking is done server-side with php uing strlen().

They will work fine so long as ONLY normal characters are used. Is there any way to make them both show the same count when forign characters are entered ?

If you load this little script you will see the problem. Try a couple of forign characters and see what I mean.

<?php

$N_test		= strip_tags(trim($_POST["test"]));

echo "<br>"."$N_test" ;
echo "<br>".strlen($N_test);

$test1		= "asbdget ght wety urlk mnga thetabdn tersl rjhts eh";
echo "<br>"."$test1" ;
echo "<br>".strlen($test1);


?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title></title>
<script type="text/javascript">
<!--

function chk_length(){
document.main_fm.opt_lg_count.value='Count = '+(document.main_fm.test.value.length)
}  // end of function


document.onkeyup=chk_length

// -->
</script>

</head>
<body>
<div style='width:300px;position:absolute;left:300px; top:300px;' >
<form name="main_fm" action="zz.php" method = 'POST'>
<textarea  rows = 18 cols = 50 name='test' " />
</textarea><br>
<input class = 'form_box'  type='TEXT' name='opt_lg_count' size = '25' />
<br>
<center><input type = 'submit' value='Submit'><br>
Submit.</center>
</form>
</div>
</body>
</html>

Thanks

    Maybe you need to use multibyte string functions:

    I was testing this theory, but multibyte string functions are not installed by default:

    http://www.php.net/manual/en/ref.mbstring.php

    However, I can't be sure this is the solution, so search around to see if anyone else that needs Turkish characters has found this useful.

      The multbyte thing seemed right.

      So I changed my php.ini to read:

      ; overload(replace) single byte functions by mbstring functions.
      ; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
      ; etc. Possible values are 0,1,2,4 or combination of them.
      ; For example, 7 for overload everything.
      ; 0: No overload
      ; 1: Overload mail() function
      ; 2: Overload str() functions
      ; 4: Overload ereg
      () functions
      mbstring.func_overload = 2

      and replaced the coding in zz.php as:

      <?php
      
      $N_test		= strip_tags(trim($_POST["test"]));
      
      echo "<br>"."$N_test" ;
      echo "<br>".mb_strlen($N_test);
      
      $test1		= "asbdget ght wety urlk mnga thetabdn tersl rjhts eh";
      echo "<br>"."$test1" ;
      echo "<br>".mb_strlen($test1);
      
      
      ?> 
      
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
      
      <html>
      <head>
      <title></title>
      <script type="text/javascript">
      <!--
      
      function chk_length(){
      document.main_fm.opt_lg_count.value='Count = '+(document.main_fm.test.value.length)
      }  // end of function
      
      
      document.onkeyup=chk_length
      
      // -->
      </script>
      
      </head>
      <body>
      <div style='width:300px;position:absolute;left:300px; top:300px;' >
      <form name="main_fm" action="zz.php" method = 'POST'>
      <textarea  rows = 18 cols = 50 name='test' " />
      </textarea><br>
      <input class = 'form_box'  type='TEXT' name='opt_lg_count' size = '25' />
      <br>
      <center><input type = 'submit' value='Submit'><br>
      Submit.</center>
      </form>
      </div>
      </body>
      </html>
      

      Still I get high counts for turkisk characters. :mad:

      A re-started Apachie - is there something else I should do ?

      Thanks.

        Have you specified the character encoding that your pages use?

        The manual page for mb_strlen says: "If encoding is omitted, internal character encoding is used."

        If that doesn't solve it, then I'm afraid you're going to have to seek out people who have had this problem with multi-language websites already.

          maybe you should specify CHARSET in your page

          I also in php.ini and in Apache have a 'default Charset',
          so that if nothing is told in <html> or in my php scripts
          the default charset is automatically set to:
          ISO-8859-1

          I dont know for sure, if this is your solution
          but I imagine different charsets may handle turkish chars differently.
          Like UTF-8 or some else charset.

          http://www.google.com/index.html
          now uses 'UTF-8', like this:

          <meta http-equiv="content-type" content="text/html; charset=UTF-8">

          I still use the default charset of 'ISO-8859-1'
          as my setting in PHP.ini and in my Apache configuration. httpd.conf

          🙂

          .

            Write a Reply...