JustTooCool Posted December 22, 2009 Share Posted December 22, 2009 I tried to make a character counter on a website that I am working on and it failed miserably. What I want to know is if you can even do it with php I am not asking for the code I want to know if it is possible so that I can get off my lazy butt and look harder. I tried using a strlen() function to do it but ...yeah. Quote Link to comment https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/ Share on other sites More sharing options...
rajivgonsalves Posted December 22, 2009 Share Posted December 22, 2009 please give an example of what you want to achieve ? Quote Link to comment https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/#findComment-982163 Share on other sites More sharing options...
Vebut Posted December 22, 2009 Share Posted December 22, 2009 There are a couple of ways to count the characters in a string (If i get your question right): 1. Use a for loop with a counter and check to see if each string index is accessible. (Have a look at "{x}" in the string manual). 2. Use regular expressions to match each character 3. Use string length function, strlen() - very simpel and very fast 4. Explode the string and count array posts. Look at str_split(). Quote Link to comment https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/#findComment-982164 Share on other sites More sharing options...
salathe Posted December 22, 2009 Share Posted December 22, 2009 I tried using a strlen() function to do it but ...yeah. But... what didn't work? Are you trying to simply count the number of characters in a string (e.g. "This is a string" has 16 characters) or the number of occurrances of characters, or...? Quote Link to comment https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/#findComment-982167 Share on other sites More sharing options...
JustTooCool Posted December 22, 2009 Author Share Posted December 22, 2009 Yeah sorry about that I should be more specific I am a little new to php and programming as a whole. Anyways for example I was trying to make a character counter underneath my textarea that would say "You have written x out of 1000 possible characters." I simply typed, //<?php $count = strlen($_post['subject']); echo $count;?> Don't laugh. lol Quote Link to comment https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/#findComment-982650 Share on other sites More sharing options...
rajivgonsalves Posted December 22, 2009 Share Posted December 22, 2009 you means how many characters left out of thousand ? Quote Link to comment https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/#findComment-982654 Share on other sites More sharing options...
premiso Posted December 22, 2009 Share Posted December 22, 2009 You want to use Javascript to display how many characters have been written in a text area. As PHP is server side it does not know until they submit the form. Quote Link to comment https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/#findComment-982655 Share on other sites More sharing options...
JustTooCool Posted December 22, 2009 Author Share Posted December 22, 2009 I was afraid of that but thanks for the advice. I thought to myself about how the information needs to be submitted before any work goes on. But you never know sometimes these programming languages can surprise you. Quote Link to comment https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/#findComment-982659 Share on other sites More sharing options...
Vebut Posted December 27, 2009 Share Posted December 27, 2009 PHP is a serverside language, meaning that it can only be used when a request is made to the server. Quote Link to comment https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/#findComment-984465 Share on other sites More sharing options...
Garethp Posted December 27, 2009 Share Posted December 27, 2009 <textarea id="post" onKeyUp="alter(this)"></textarea> <div id="Ch">You have used 0 out of 1000 characters</div> <script type="javascript/text"> function alter(textarea) { var x; x = textarea.value; x = x.length(); document.getElementById('Ch').innerHTML = "You have used " + x + " out of 1000 characters"; } </script> Something like that should work Quote Link to comment https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/#findComment-984469 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.