PHP help for text color processing

I play a MUD (Multi User Dimension) game (a text only game very much, otherwise, similar to World Of Warcraft).
In this text only game, colored text is output to the client using tags like {Y for bright Yellow, {y for subdued yellow and {x to return to default color. There is about a dozen options for colors.

I would like to create a php function to duplicate this functionality as closely as possible so that when I code a webpage I can do this directly in the html (after a function call?).

Please help. I have a basic understanding of coding and can pull apart and use other peoples’ code but I’m new to php.

You asking for some pretty complicated string manipulation.

If you think about it, html doesn’t work like this, turn on one color until the parser reaches another {color character and then switch to that color for the next duration of characters until it reaches another {color tag. If we were talking about taking plain text and then converting it to html with this color scheme, that would be different but in this case, your parser will have to look for


, and any other html block nodes to insert the current color as style=“color:[color];”. Then if the color changed in the middle of one of these nodes you would have to insert a , only if the latest color changes was defined in a parent node and if it was defined by another span tag then you would first have to insert a closing span tag like so .

This is going to require a lot of logic and a lot of testing and debugging for every html scenario or to even get to a point where you can say that it has a 90% success rate. I recommend that you do some searching around to try to determine which parsing engine this game uses or to see if someone has already written something for this sort of thing, chances are someone already has and you just haven’t found it yet.

Sponsor our Newsletter | Privacy Policy | Terms of Service