0

Is there a way that I can convert these strings into HTML tags and vice versa?

example:

$str = 'The^ff0000 quick brown^000000 fox jumps over the lazy dog.'

the output must be

The<span style="color:#ff0000;"> quick brown</span> fox jumps over the lazy dog.

something like that and vice versa

2
  • No one seems to have much success answering your questions. Commented Jul 24, 2012 at 16:10
  • 1
    you should probably take the time to learn to write parsers (dragon book is nice for that). Commented Jul 24, 2012 at 16:10

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

You have a start and an end ^ ; And what about nested ones!
Presumably he wants the ff0000 to become ff00ff if the string contains ^ff00ff. I think it's more difficult than you're making it.
That's great Brett thanks, but how about reconverting it to ^ff0000 EDIT: Solved it already with $str = str_replace('</span>', '^000000', $str); $str = preg_replace('@\<span style="color:#([a-f\d]{6});">@i', '^$1', $str);
Another question is what if a span contains parameters other than color like <span style="color:#ffffff;text-align:center;font-weight:bold;etc..."></span>... it doesn't convert
If the color is always at the beginning, you can just remove ;"> from the preg_replace. If you get too complicated in your needs, you may need to go to some kind of HTML parser since regular expressions (as I am using) are not at all ideal for parsing HTML, especially if you want to handle all of the edge cases.
|
0

Sure it is a simple string replace if the ^(non-000000) value always represents an opening span tag with the hex value being the color style and a ^000000 always means a closing span tag.

Comments

0

This is a good example in which you should implement a Decorator patern (ex: make a static method and process the string using explode for instance and append the spans)

Comments

0

Since you have no actual closing tag, and would like ^000000 to represent the span closing tag, I'll expand on Mike's answer below.

$string = "The^ff0000 quick brown^000000 fox jumps over the lazy dog.";

// replace '^000000' first so we don't get false positives in the regex
$new_string = str_replace('^000000', '</span>', $string);

$new_string = preg_replace('/\^([0-9a-fA-F]+)\b/i', '<span style="color: #$1;">', $new_string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.