Jump to content

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.