PHP abstract state-based string parser