440,569 Members | 1,422 Online
Need help? Post your question and get tips & solutions from a community of 440,569 IT Pros & Developers. It's quick & easy.

# String conversion algorithm

 P: n/a Hi. I'm trying to make a conversion algorithm that colors even and odd words in a HTML string with
tags. // input text with all sorts of HTML tags and whitespace \$str = "

Title here

This is a nice
and some nice text

"; // color the words and echo echo color_words(\$str); Would like the output to be:

Title
here

This
is
a
nice

and
some
nice
text

I tried it with some nested for and while loops, but kept getting infinite loops, char. skipping problems, etc. Maybe regular expressions is the answer, but I have very little (and bad) experience with those. I'm sure there are lots of you would could solve this in a couple of seconds; please do so and help me out! :) Thanks, - John Jul 17 '05 #1
8 Replies

 P: n/a "John van Terheijde" wrote: Hi. I’m trying to make a conversion algorithm that colors even and odd words in a HTML string with
tags. // input text with all sorts of HTML tags and whitespace \$str = "

Title here

This is a nice
and some nice text

"; // color the words and echo echo color_words(\$str); Would like the output to be:

Title
here

This
is
a
nice

and
some
nice
text

I tried it with some nested for and while loops, but kept getting infinite loops, char. skipping problems, etc. Maybe regular expressions is the answer, but I have very little (and bad) experience with those. I’m sure there are lots of you would could solve this in a couple of seconds; please do so and help me out! Thanks, - John I give you a non-exact solution: \$array = explode(" ", \$str); //you can use "split" for finer splitting foreach (\$array as \$key => \$word) { //detect odd/even if (intval(\$key/2) == \$key/2) \$word = "" . \$word . "<...>"; else \$word = "the other color" . \$word . "other color stuff" } \$out = implode(" ", \$array); echo \$out; Hope this helps get you started -- http://www.dbForumz.com/ This article was posted by author's request Articles individually checked for conformance to usenet standards Topic URL: http://www.dbForumz.com/PHP-String-c...ict133295.html Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=445292 Jul 17 '05 #2

 P: n/a this code has one small bug - works with empty words try to convert this: "evenword1 oddword1 evenword2 oddword2" (notice two spaces between oddword1 and evenword2) it produces this result: "evenword1 oddword1 evenword2 oddword2" but we need this result: "evenword1 oddword1 evenword2 oddword2" so try small correcture: [PHP] \$array = explode(" ", \$str); //you can use "split" for finer splitting \$i = 1; foreach (\$array as \$key => \$word) if(\$word != "") { if (\$i%2 == 0) \$word = "" . \$word . "<...>"; else \$word = "the other color" . \$word . "other color stuff" \$i++; } [/PHP] "steve" píąe v diskusním příspěvku news:10*************@news.supernews.com... "John van Terheijde" wrote: > Hi. > > I’m trying to make a conversion algorithm that colors even and > odd words in > a HTML string with
tags. > > // input text with all sorts of HTML tags and whitespace > \$str = "

Title here

>

This is a nice src="picture.gif" />
> and some nice text

"; > > // color the words and echo > echo color_words(\$str); > > Would like the output to be: >

Title
class="c2">here

>

This
class="c2">is
class="c1">a
class="c2">nice
src="picture.gif" />
>
and
class="c2">some
class="c1">nice
text
>

> > I tried it with some nested for and while loops, but kept getting > infinite > loops, char. skipping problems, etc. > > Maybe regular expressions is the answer, but I have very little (and > bad) > experience with those. > > I’m sure there are lots of you would could solve this in a > couple of > seconds; please do so and help me out! > > Thanks, > > - John I give you a non-exact solution: \$array = explode(" ", \$str); //you can use "split" for finer splitting foreach (\$array as \$key => \$word) { //detect odd/even if (intval(\$key/2) == \$key/2) \$word = "" . \$word . "<...>"; else \$word = "the other color" . \$word . "other color stuff" } \$out = implode(" ", \$array); echo \$out; Hope this helps get you started -- http://www.dbForumz.com/ This article was posted by author's request Articles individually checked for conformance to usenet standards Topic URL: http://www.dbForumz.com/PHP-String-c...ict133295.html Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=445292 Jul 17 '05 #3

 P: n/a using some loop is good idea, it's simple :)) try something like this, you can enhance it to detect more separators (not only space), to ignore HTML tags and so on... [PHP] \$src = "evenword1 oddword1 evenword2 oddword2"; \$tmp = \$src; \$dst = ""; \$i = 0; do{ \$pos = strpos(\$tmp, " "); if(\$pos !== false) // separator found, we have "word" { \$word = substr(\$tmp, 0, \$pos); // extract word if(\$word != "") // isn't it empty?? { if(\$i%2 == 0) \$word = "".\$word.""; // colorize it else \$word = "".\$word.""; \$i++; // increase word counter } \$dst .= \$word.substr(\$tmp, \$pos, 1); // append colorized word and separator to destination \$tmp = substr(\$tmp, \$pos+1); // and trim used part from temporary } else // no other separator found, we have last word { if(\$tmp != "") // isn't it empty?? { if(\$i%2 == 0) \$tmp = "".\$tmp.""; // colorize it else \$tmp = "".\$tmp.""; \$i++; // increase word counter } \$dst .= \$tmp; unset(\$tmp); break; } }while(1); echo "
"; echo "source: ".htmlspecialchars(\$src)."
"; echo "destination: ".htmlspecialchars(\$dst)."
"; echo "we have ".\$i." words"; echo "
"; [/PHP] "John van Terheijden" píąe v diskusním příspěvku news:ce**********@news5.tilbu1.nb.home.nl... Hi. I'm trying to make a conversion algorithm that colors even and odd words in a HTML string with
tags. // input text with all sorts of HTML tags and whitespace \$str = "

Title here

This is a nice
and some nice text

"; // color the words and echo echo color_words(\$str); Would like the output to be:

Title
here

This
is
a
nice

and
some
nice
text

I tried it with some nested for and while loops, but kept getting infinite loops, char. skipping problems, etc. Maybe regular expressions is the answer, but I have very little (and bad) experience with those. I'm sure there are lots of you would could solve this in a couple of seconds; please do so and help me out! :) Thanks, - John Jul 17 '05 #4

 P: n/a Thanks for the replies! The given solutions didn't do anything with the fact that shouldn't be colored. I fiddled some more with my initial for loop and came up with a solution. The main difference between this and earlier attempts is that I now haven't got nested for and while loops, so every time only 1 character is handled. function color(\$string) { \$whitespace = explode('-', " -\n-\r- -\0-\x0B"); \$out = ''; \$color1 = true; \$inTag = false; \$inColor = true; for (\$i = 0; \$i < strlen(\$string); \$i++) { \$char = \$string[\$i]; // in a tag if (\$inTag) { if (\$char == '>') \$inTag = false; } // not in a tag else { // starting a tag if (\$char == '<') { \$inTag = true; if (\$inColor) { \$out .= ''; \$inColor = false; } } // in whitespace elseif (in_array(\$char, \$whitespace)) { if (\$inColor) { \$out .= ''; \$inColor = false; } } // in "normal" text else { if (!\$inColor) { if (\$color1) \$out .= ''; else \$out .= ''; \$color1 = !\$color1; \$inColor = true; } } } \$out .= \$char; } \$out .= ''; return \$out; } Thanks, - John "John van Terheijden" schreef in bericht news:ce**********@news5.tilbu1.nb.home.nl... Hi. I'm trying to make a conversion algorithm that colors even and odd words in a HTML string with
tags. // input text with all sorts of HTML tags and whitespace \$str = "

Title here

This is a nice
and some nice text

"; // color the words and echo echo color_words(\$str); Would like the output to be:

Title
here

This
is
a
nice

and
some
nice
text

I tried it with some nested for and while loops, but kept getting infinite loops, char. skipping problems, etc. Maybe regular expressions is the answer, but I have very little (and bad) experience with those. I'm sure there are lots of you would could solve this in a couple of seconds; please do so and help me out! :) Thanks, - John Jul 17 '05 #5

 P: n/a .oO(John van Terheijden) The given solutions didn't do anything with the fact that shouldn'tbe colored. I fiddled some more with my initial for loop and came up with asolution. [...] But there are some bugs, the beginning and end of your teststring look like this:

Title ... ^^^^^^^^^^^^^ .... text

^^^^^^^ I've played around a bit with regular expressions: function color_words(\$string) { \$color = 2; \$pattern = '#[^\s<>]+(?![^<]*>)#e'; \$replace = '"\$0"'; return preg_replace(\$pattern, \$replace, \$string); } With my teststrings it works, but it's quite possible that there are situations where it'll fail, you have to test it. Micha Jul 17 '05 #6

 P: n/a Hi Michael. Thanks for the reply. You were right about the "bugs". The opening and closing were needed in earlier attempts. I solved it better now: --- function color(\$string) { \$whitespace = explode('-', " -\n-\r-\t-\0-\x0B"); \$color1 = true; \$inTag = false; \$inColor = false; for (\$i = 0; \$i < strlen(\$string); \$i++) { \$char = \$string[\$i]; // in a tag if (\$inTag) { if (\$char == '>') \$inTag = false; } // not in a tag else { // starting a tag if (\$char == '<') { \$inTag = true; if (\$inColor) { \$out .= ''; \$inColor = false; } } // in whitespace elseif (in_array(\$char, \$whitespace)) { if (\$inColor) { \$out .= ''; \$inColor = false; } } // in "normal" text else { if (!\$inColor) { if (\$color1) \$out .= ''; else \$out .= ''; \$color1 = !\$color1; \$inColor = true; } } } \$out .= \$char; } if (\$inColor) \$out .= ''; return \$out; } --- I tested your code and with my test HTML it works just fine. I think I'll use your code now, for it's nice and short (and prob. faster) and switch back to mine when changes or additional stuff is needed :) Thanks! - John "Michael Fesser" schreef in bericht news:38********************************@4ax.com... .oO(John van Terheijden)The given solutions didn't do anything with the fact that shouldn'tbe colored. I fiddled some more with my initial for loop and came up with asolution. [...] But there are some bugs, the beginning and end of your teststring look like this:

Title ... ^^^^^^^^^^^^^ ... text

^^^^^^^ I've played around a bit with regular expressions: function color_words(\$string) { \$color = 2; \$pattern = '#[^\s<>]+(?![^<]*>)#e'; \$replace = '"\$0"'; return preg_replace(\$pattern, \$replace, \$string); } With my teststrings it works, but it's quite possible that there are situations where it'll fail, you have to test it. Micha Jul 17 '05 #7

 P: n/a .oO(John van Terheijden) You were right about the "bugs". The opening and closing wereneeded in earlier attempts. I solved it better now: Seems to work, but there are still some little issues: function color(\$string) { \$whitespace = explode('-', " -\n-\r-\t-\0-\x0B"); \$color1 = true; \$inTag = false; \$inColor = false; You should add an \$out = ''; at the beginning, or you'll get a notice on a line with \$out .= ... (if error_reporting is set to E_ALL, which should be the case on development systems). for (\$i = 0; \$i < strlen(\$string); \$i++) { \$char = \$string[\$i]; Use curly braces for accessing single chars in a string: \$char = \$string{\$i}; Using array-brackets for this is still allowed for backwards compatibility, but deprecated. I tested your code and with my test HTML it works just fine. I think I'lluse your code now, for it's nice and short (and prob. faster) [...] I have done a little test on my "server" (P100, 64MB RAM ;) ): With your given teststring the regular expression method was 2-3 times faster, with a longer string (a complete webpage) it was still 2 times faster. Micha Jul 17 '05 #8

 P: n/a You're right about the notice and the curly braces. I didn't know about them, it worked with [], so I thought that was the way.. Thanks for the testing! - John "Michael Fesser" schreef in bericht news:la********************************@4ax.com... .oO(John van Terheijden)You were right about the "bugs". The opening and closing wereneeded in earlier attempts. I solved it better now: Seems to work, but there are still some little issues:function color(\$string) { \$whitespace = explode('-', " -\n-\r-\t-\0-\x0B"); \$color1 = true; \$inTag = false; \$inColor = false; You should add an \$out = ''; at the beginning, or you'll get a notice on a line with \$out .= ... (if error_reporting is set to E_ALL, which should be the case on development systems). for (\$i = 0; \$i < strlen(\$string); \$i++) { \$char = \$string[\$i]; Use curly braces for accessing single chars in a string: \$char = \$string{\$i}; Using array-brackets for this is still allowed for backwards compatibility, but deprecated.I tested your code and with my test HTML it works just fine. I think I'lluse your code now, for it's nice and short (and prob. faster) [...] I have done a little test on my "server" (P100, 64MB RAM ;) ): With your given teststring the regular expression method was 2-3 times faster, with a longer string (a complete webpage) it was still 2 times faster. Micha Jul 17 '05 #9

### This discussion thread is closed

Replies have been disabled for this discussion.