Solution: 1
You can count characters in a string (excluding certain characters) using regular expressions with preg_match_all().
$value = "This is a test string.";
// Count all characters except spaces
$length = preg_match_all('/[^ ]/', $value, $matches);
echo $length; // Output: 18
Excluding multiple characters
If you want to exclude specific characters (e.g., spaces and i):
$value = "This is a test string.";
// Exclude spaces and "i"
$length = preg_match_all('/[^ i]/', $value, $matches);
echo $length; // Output: 15
Escaping special characters
Some characters (.^$*+?()[{\|) must be escaped with a backslash. For example, to exclude spaces and the dot (.):
$value = "This is a test string.";
// Exclude spaces and "."
$length = preg_match_all('/[^ \.]/', $value, $matches);
echo $length; // Output: 18