
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Number of Characters in a String in PHP
To find the number of characters in the string, the PHP code is as follows −
Example
<?php $my_string = "Hi there, this is a sample "; $len = mb_strlen($my_string); print_r("The number of characters in the string is "); echo $len; ?>
Output
The number of characters in the string is 27
Above, a string is defined that has more than the required spaces in between −
$my_string = "Hi there, this is a sample ";
Next, the length of the string is computed using the ‘strlen’ function. This length is assigned to a variable. This will give the characters present in the string including the spaces as well −
$len = mb_strlen($my_string);
The length computed is then printed on the screen −
print_r("The number of characters in the string is "); echo $len;
Advertisements