Jump to content

Can you make a chracter counter wih PHP?


JustTooCool

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/185997-can-you-make-a-chracter-counter-wih-php/
Share on other sites

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().

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...?

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

<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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.