Replace in place, parsing & string manipulation.

Posted by Mark Tomlin on Stack Overflow See other posts from Stack Overflow or by Mark Tomlin
Published on 2010-03-23T15:41:26Z Indexed on 2010/03/23 15:43 UTC
Read the original article Hit count: 384

I'm trying to replace a set of characters within a string. The string may or may not have any data to change. The string is marked up in a way that allows for it to change it's color from a set of characters. The string can reset it's it's formatting to default by using a defined set of characters.

This setup is very much like the ECMA-48 standard used on LINUX consoles for colors and other special effects.

Where one string could be ^0Black^1Red^2Green^3Yellow^4Blue^5Purple^6Cyan^7White Producing the following HTML:

<span style="color: #000">Black</span><span style="color: #F00">Red</span><span style="color: #0F0">Green</span><span style="color: #FF0">Yellow</span><span style="color: #00F">Blue</span><span style="color: #F0F">Purple</span><span style="color: #0FF">Cyan</span><span style="color: #FFF">White</span>

Another string (^1Error^8: ^3User Error) could also produce:

<span style="color: #F00">Error</span>: <span style="color: #FF0">User Error</span>

You might of noticed the ^8 part of that string resets the color for that part of the string.

What's the best way to go about parsing these kinds of strings?

© Stack Overflow or respective owner

Related posts about php

Related posts about strings