How to find number of characters or length of a string in PHP

There are a lot of 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 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, number of characters of which we want to count. PHP string function strlen($str) counts the number of characters. Let us save this as index.php in a folder "char_count" under xampp/htdocs.

We will now test it.

character count in phpTest the Application

Run localhost/char_count in the browser. You must start the Apache web server in XAMPP control panel. You will see the page opened with count displayed in blue color.

Character Count in PHP

It is counts the spaces also. If you want to count number of characters without spaces, you can change 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 counts including spaces as well as 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 and then counting for without spaces.

If you test it now, it will display both the counts, with spaces and without spaces. See below output:

Character Count in PHP

number of characters in string in php Note

  1. 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 that a string can have tab spaces also. In that case this example will not work, because we are removing spaces only. To remove tabs, you can use preg_replace() function.
  2. If the input string is empty, strlen() will return 0, which means number of characters in the string is 0.

php length of a stringConclusion

This is an example of using string function in PHP. There are many useful string functions available in PHP, you can use them during PHP programming. Purpose of this example was to show how you can use PHP string function while you are practicing PHP program.