If you are only talking about a few specific codes, you could use:
$str = "The^ff0000 quick brown^000000 fox jumps over the lazy dog.";
$str = str_replace('^ff0000', '<span style="color:#ff0000;">', $str);
$str = str_replace('^000000', '</span>', $str);
or if you prefer:
$str = "The^ff0000 quick brown^000000 fox jumps over the lazy dog.";
$str = str_replace(array('^ff0000', '^000000'), array('<span style="color:#ff0000;">', '</span>'), $str);
If you wish to allow any number of color codes, you might do:
$str = str_replace('^000000', '</span>', $str);
$str = preg_replace('@\^([a-f\d]{6})@i', '<span style="color:#$1;">', $str);
For a conversion back (if you are not using any other </span>'s), it could be:
$str = str_replace('</span>', '^000000', $str);
$str = preg_replace('@<span style="color:#([a-fA-F\d]{6});">@', '^$1', $str);
Note that this assumes you are typing the <span> exactly as above, without variability in whitespace.