Parsing multibyte string in PHP

Posted by Petr Peller on Stack Overflow See other posts from Stack Overflow or by Petr Peller
Published on 2010-04-07T08:36:23Z Indexed on 2010/04/07 8:43 UTC
Read the original article Hit count: 376

Filed under:
|
|
|

I would like to write a (HTML) parser based on state machine but I have doubts how to acctually read/use an input. I decided to load the whole input into one string and then work with it as with an array and hold its index as current parsing position.

There would be no problems with single-byte encoding, but in multi-byte encoding each value does not represent a character, but a byte of a character.

Example:

$mb_string = 'žšcr'; //4 multi-byte characters in UTF-8

for($i=0; $i < 4; $i++)
{
   echo $mb_string[$i], PHP_EOL;
}

Outputs:

L
ž
L
A

This means I cannot iterate through the string in a loop to check single characters, because I never know if I am in the middle of an character or not.

So the questions are:

  • How do I multi-byte safe read a single character from a string in a performance friendly way?
  • Is it good idea to work with the string as it was an array in this case?
  • How would you read the input?

© Stack Overflow or respective owner

Related posts about php

Related posts about parsing