This is one of the simple method to calculate how many words in the particular textarea. We will done this concept with the help of php str_word_count() function.
I used here is one of the simple form, in this form contain one textarea field and one submit button after the form post i stored the full string of the textarea into the $count variable then i used the str_word_count function to that. Then i displayed the count in h1 tag. Check out the below demo page how it works. I hope this is helpful.
Code for word counter:
<?php
if(isset($_POST['submit']))
{
$count = $_POST['count'];
$word_count = str_word_count($count);
echo '<h1 style="color:green">Total Words: ', $word_count; echo '</h1>';
}
?>
<style>
.count
{
background-color:green;
color:#fff;
border:2px solid green;
padding:15px;
width:200px;
margin-top:15px;
}
textarea
{
width:100%;
}
</style>
<html>
<head>
<title>Word counter using php</title>
</head>
<body>
<h1>word counter using php str_word_count() function:</h1>
<form action="" method="post">
<textarea name="count" rows="18"> </textarea><br/>
<input type="submit" name="submit" value="count" class="count" />
</form>
</body>
</html>
if(isset($_POST['submit']))
{
$count = $_POST['count'];
$word_count = str_word_count($count);
echo '<h1 style="color:green">Total Words: ', $word_count; echo '</h1>';
}
?>
<style>
.count
{
background-color:green;
color:#fff;
border:2px solid green;
padding:15px;
width:200px;
margin-top:15px;
}
textarea
{
width:100%;
}
</style>
<html>
<head>
<title>Word counter using php</title>
</head>
<body>
<h1>word counter using php str_word_count() function:</h1>
<form action="" method="post">
<textarea name="count" rows="18"> </textarea><br/>
<input type="submit" name="submit" value="count" class="count" />
</form>
</body>
</html>
0 Comments