Count Number of Characters in PHP Free Source code and Tutorial
Introduction
This article will guide you on how to count the number of characters based on the user input. It is easy to follow and understand. This tutorial is a great way to learn how to count the number of characters using PHP. We will provide source code and explain the relevant lines in the source code for you to better understand how it works. We would recommend this tutorial to anyone who wants to learn how to count the number of characters in PHP.
Objectives
By the end of this tutorial, you will be able to:
- To create a PHP script that will count the number of characters based on user input.
- Use strlen() function in PHP.
- To integrate and apply the concept of this tutorial in your projects.
Relevant Source code
The following are the requirements for this tutorial:
- XAMPP
- Text editor (VS Code, Sublime, Brackets), download and install a text editor of your choice
Front-end
Line 19-23 – this is the form used to capture the user input. It consists of textbox and submit button.
<form method="post"> <input type="text" name="word" class="form-control" placeholder="Enter a word"> <br/> <input type="submit" name="submit" class="btn btn-primary" value="Count Characters"> </form>
Back-end
Line 24-31 – this is the PHP script that will process the submitted information by the user through the form presented earlier. The script will only be executed if the user clicks the Count Characters button.
The strlen() function is a PHP built-in function that returns the length of a string. It accepts a string as a parameter and returns the length of the string. It determines how long the string is, taking into account all whitespace and special characters.
strlen(string) – this is the syntax of the strlen() function. It has one parameter and that is the string to be check. In our case it is the $str variable wherein it stores the data entered by the user in the textbox.
<?php if(isset($_POST['submit'])){ $str = $_POST['word']; $len = strlen($str); echo "<br><h4>Input Word: ". $str."</h4><br>"; echo "<h4>Number of Characters: ".$len."</h4>"; } ?>
Video Demo
Complete Source code
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no"> <title>Count Number of Characters in PHP</title> <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css"> </head> <body> <div class="row"> <div class="col-md-8 offset-md-2"> <div class="card"> <div class="card-header bg"> <h1>Count Number of Characters in PHP</h1> </div> <div class="card-body"> <form method="post"> <input type="text" name="word" class="form-control" placeholder="Enter a word"> <br/> <input type="submit" name="submit" class="btn btn-primary" value="Count Characters"> </form> <?php if(isset($_POST['submit'])){ $str = $_POST['word']; $len = strlen($str); echo "<br><h4>Input Word: ". $str."</h4><br>"; echo "<h4>Number of Characters: ".$len."</h4>"; } ?> </div> </div> </div> </div> <script src="assets/js/jquery.min.js"></script> <script src="assets/bootstrap/js/bootstrap.min.js"></script> </body> </html>
Summary
The count of characters can be useful in a number of ways. For example, if you need to find out how many characters are in a particular string of text, you can use the count of characters function. This can be useful for a number of reasons, such as determining how much text will fit into a given space, or for finding specific strings of text. Additionally, the count of characters can be used to check for valid input. For example, it can be used in the comment section wherein the users or site visitors can only post a comment using limited number of characters. Overall, the count of characters is a useful function that can be used in a variety of ways.
We hope you found this tutorial to be helpful! Wishing you the best of luck with your projects! Happy Coding!
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.
Related Topics and Articles:
How to Generate Random Password in PHP Free Source code and Tutorial
Calculate Sum of Column in PHP and MySQL
Image Upload in PHP and MySQL Free Tutorial and Source code
Password Indicator in PHP Free Source code and Tutorial
Browser Detector Script in PHP Free Source code and Tutorial