Jump to content

Word Count


takeiteasy

Recommended Posts

Hi all! i need some help here, hope some of u can help!

 

Can someone help me with generating the number of word counts for the following code? That means when i type the words in the textbox, another textbox will show the number of words typed. i'm using php code.


Information <b>(max of 50 words):</b>
      <tr>
        <td class="td_normal" colspan="2">
          <input class="td_normal" type="text" size="50" name="content2" ';
          if ($FormXEmployerObj)
          $str.='value="'.$FormXEmployerObj->get(content2).'"';
          $str.='>
          <br>
Current Word Count:<tr>
          <td class="td_normal" colspan="2">
          <input class="td_normal" type="text" size="2" name="content3"';
          if ($FormXEmployerObj)
          $str.='value="'.$FormXEmployerObj->get(content3).'"';
          $str.='>
        </td>
      </tr>

 

Thanks!!

Link to comment
Share on other sites

<?php

function wordcount($string) {
$pieces = explode(" ", $string);
return count($pieces);
}

$string = "word1 word2 word3 word4";
$count = wordcount($string);

echo "The string \"{$string}\" has {$count} words!";

?>

 

Can be adapted to suit your needs :)

 

Link to comment
Share on other sites

Thanks chigley for your help, but i'm quite new to php...so i dont really know how to adapt my current code with the 1 u gave me..can u pls guide me. thanks!!

 

A couple things to consider: simply exploding on the spaces, while a great start, is very easily tricked. If you were to type something like a 5 : !, it would be counted as 4 words.

 

Secondly, if you're wanting it to be a live count, you may want to use javascript instead.

Link to comment
Share on other sites

Thanks obsidian for your help, but is there a code that will count the number of words while u type in the textbox? that means we do not need to click the count word button n we'll know the number of words.

 

Once you get the word count function working, you'll need to tie it to the onkeypress event on your textarea. Then, you'll need to modify your function slightly to know which area in which to count the words.

Link to comment
Share on other sites

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.