PASSWORD RESET

Your destination for complete Tech news

PHP

How to count the number of characters in a string in PHP?

631 0
< 1 min read

To count the number of characters in a string in PHP, you can use the strlen() function.

This function takes a string as argument and returns the number of characters in the string, including spaces and punctuation.

Here is an example of how to use the strlen() function to count the number of characters in a string:

$string = "This is a test string.";
$num_chars = strlen($string);

echo $num_chars; // Outputs: 24

If you want to count the number of characters in a string, excluding spaces and other white space characters, you can use the trim() function to remove the leading and trailing white space from the string, and then use the strlen() function to count the remaining characters.

Here is an example of how to count the number of characters in a string, excluding spaces and other white space characters:

$string = " This is a test string. ";
$string = trim($string);
$num_chars = strlen($string);

echo $num_chars; // Outputs: 22

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.