Skip to content
Advertisement

PHP – how to count the number of leading spaces in a multi-byte / UTF-8 string correctly

I have UTF-8 strings such as those below:

            21st century 

      Other languages 

         General collections 

         Ancient languages 

         Medieval languages 

            Several authors (Two or more languages) 

As you can see, the strings contain alphanumeric characters as well leading and trailing spaces.

I’d like to use PHP to retrieve the number of leading spaces (not trailing spaces) in each string. Note that the spaces might be non-standard ASCII spaces. I tried using:

JavaScript

where the $space_char contains a sample space character I copied from one of the above strings, and I got 160 rather than 32.

I have tried:

JavaScript

but I get a very unpredictable value.

The values should be:

JavaScript

What am I doing wrong?

Advertisement

Answer

I would go the regular expression route:

JavaScript

Output:

(1) 12
(2) 6
(3) 9
(4) 9
(5) 9
(6) 12
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement