game.over
game.over

Reputation: 21

How to use php str_word_count to count words in a textarea

I am in an entry level php course. The lesson is to create an HTML page with a textarea submit form in the essence of charging a customer for a classified ad using the number of words as the cost. That part is complete. The second page is a php script using str_word_count. How do you count the words in a textarea and then charge a price per word for every word over a set number?

Upvotes: 0

Views: 7955

Answers (3)

anon
anon

Reputation:

How about just use $words= str_word_count($_POST['clientdetails']); then, probably just do a simple maths like

$fee = $words * 0.50;

0.50 is the amount of money you are going to charge

then, something like:

<?php echo 'we are going to charge you '.$fee.' for these words'; ?>

Upvotes: 0

fab4am
fab4am

Reputation: 56

Here's the doc for str_word_count

Using it is really easy. For example if your textarea is named "mytextarea" and you sent the form via POST (<form method="POST">), your textarea data will be in$_POST['mytextarea']

Then counting the words :

$count = str_word_count($_POST['mytextarea']);

Then you just have to use the $count variable to do whatever you want.

Upvotes: 2

c.pramod
c.pramod

Reputation: 606

$var =textareaObject.value ; //Store textarea text in $var 

print_r(str_word_count($var));

Upvotes: 0

Related Questions