There are many useful string functions available in PHP. You can use them to meet many functionalities while programming in PHP in your project. One such function is strlen() in PHP which gives the number of characters in a string or length of a string.
In this topic, we will write a simple PHP program to display the number of characters in a string. See the below code to get the string length or number of characters:
$str = "This is a string";
$count = strlen($str);
See the below code:
<html>
<head>
<title>
Count number of characters in a string using PHP
</title>
</head>
<body style="text-align: center;">
<h1 >Count number of characters in a String</h1>
<?php
$str = "This is a string";
$count = strlen($str);
echo "<h2>Number of characters in the string - \"$str\" is = <span style='color:blue;'>".$count."</span> (with spaces)</h2>";
?>
</body>
</html>
We are using a PHP variable $str to hold the input string, the number of characters we want to count. PHP string function strlen($str)
counts the number of characters in the string $str. Let us save this as index.php
in the folder "char_count" under xampp/htdocs.
We will now test it.
Test the Application
Run localhost/char_count in the browser. You must start the Apache web server in the XAMPP control panel. You will see the page opened with the count displayed in blue color.
It counts the spaces also. If you want to count the number of characters without spaces, you can change it a little bit as below:
$str = str_replace(" ","",$str);
$count = strlen($str);
Before using strlen()
function, we used str_replace()
function to remove spaces. Now see the modified code for index.php
. We want to display the counts including spaces and the count without spaces.
<html>
<head>
<title>
Count number of characters in a string using PHP
</title>
</head>
<body style="text-align: center;">
<h1 >Count number of characters in a String</h1>
<?php
$str = "This is a string";
$str1 = str_replace(" ","",$str);
$count = strlen($str);
$count1 = strlen($str1);
echo "<h2>Number of characters in the string - \"$str\" is = <span style='color:blue;'>".$count."</span> (with spaces)</h2>";
echo "<h2>Number of characters in the string - \"$str\" is = <span style='color:blue;'>".$count1."</span> (without spaces)</h2>";
?>
</body>
</html>
You can see we have added str_replace()
PHP function before counting the characters without spaces.
If you test it now, it will display both the counts, with spaces and without spaces. See the below output:
Note
- While working in the project you might come across requirements to count the number of characters in a string with/without spaces or to remove spaces in a string. So, you can use these functions wherever applicable. One point you must note is that a string can have tab spaces also. In that case, this example will not work, because we are removing the spaces only. To remove tabs, you can use
preg_replace()
function. - If the input string is empty, strlen() will return 0, which means number of characters in the string is 0.
Conclusion
This is an example of using the string function in PHP. There are many useful string functions available in PHP, you can use them during PHP programming.
Post a Comment