By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,569 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
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 <div> tags.

// input text with all sorts of HTML tags and whitespace
$str = "<h1>Title here</h1>
<p>This is a <strong>nice</strong> <img src="picture.gif" /><br>
and some nice text </p>";

// color the words and echo
echo color_words($str);

Would like the output to be:
<h1><div class="c1">Title</div> <div class="c2">here</div></h1>
<p><div class="c1">This</div> <div class="c2">is</div> <div
class="c1">a</div> <strong><div class="c2">nice</div></strong> <img
src="picture.gif" /><br>
<div class="c1">and</div> <div class="c2">some</div> <div
class="c1">nice</div> <div class="c2">text</div> </p>

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
Share this Question
Share on Google+
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 <div> tags.

// input text with all sorts of HTML tags and whitespace
$str = "<h1>Title here</h1>
<p>This is a <strong>nice</strong> <img
src="picture.gif" /><br>
and some nice text </p>";

// color the words and echo
echo color_words($str);

Would like the output to be:
<h1><div class="c1">Title</div> <div
class="c2">here</div></h1>
<p><div class="c1">This</div> <div
class="c2">is</div> <div
class="c1">a</div> <strong><div
class="c2">nice</div></strong> <img
src="picture.gif" /><br>
<div class="c1">and</div> <div
class="c2">some</div> <div
class="c1">nice</div> <div class="c2">text</div>
</p>

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 = "<color...>" . $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: "<evencolor>evenword1</...>
<oddcolor>oddword1</...> <evencolor></...> <oddcolor>evenword2</...>
<evencolor>oddword2</...>"
but we need this result: "<evencolor>evenword1</...>
<oddcolor>oddword1</...> <evencolor>evenword2</...>
<oddcolor>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 = "<color...>" . $word . "<...>";
else $word = "the other color" . $word . "other color stuff"
$i++;
}
[/PHP]
"steve" <Us************@dbForumz.com> 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 <div> tags.
>
> // input text with all sorts of HTML tags and whitespace
> $str = "<h1>Title here</h1>
> <p>This is a <strong>nice</strong> <img
> src="picture.gif" /><br>
> and some nice text </p>";
>
> // color the words and echo
> echo color_words($str);
>
> Would like the output to be:
> <h1><div class="c1">Title</div> <div
> class="c2">here</div></h1>
> <p><div class="c1">This</div> <div
> class="c2">is</div> <div
> class="c1">a</div> <strong><div
> class="c2">nice</div></strong> <img
> src="picture.gif" /><br>
> <div class="c1">and</div> <div
> class="c2">some</div> <div
> class="c1">nice</div> <div class="c2">text</div>
> </p>
>
> 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 = "<color...>" . $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 = "<color1>".$word."</color1>"; // colorize it
else $word = "<color2>".$word."</color2>";
$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 = "<color1>".$tmp."</color1>"; // colorize it
else $tmp = "<color2>".$tmp."</color2>";
$i++; // increase word
counter
}
$dst .= $tmp;
unset($tmp);
break;
}
}while(1);

echo "<pre>";
echo "source: ".htmlspecialchars($src)."<br>";
echo "destination: ".htmlspecialchars($dst)."<br>";
echo "we have ".$i." words";
echo "</pre>";
[/PHP]
"John van Terheijden" <va************************@home.nl> 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 <div> tags.

// input text with all sorts of HTML tags and whitespace
$str = "<h1>Title here</h1>
<p>This is a <strong>nice</strong> <img src="picture.gif" /><br>
and some nice text </p>";

// color the words and echo
echo color_words($str);

Would like the output to be:
<h1><div class="c1">Title</div> <div class="c2">here</div></h1>
<p><div class="c1">This</div> <div class="c2">is</div> <div
class="c1">a</div> <strong><div class="c2">nice</div></strong> <img
src="picture.gif" /><br>
<div class="c1">and</div> <div class="c2">some</div> <div
class="c1">nice</div> <div class="c2">text</div> </p>

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 <tags> 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 = '<span>';
$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 .= '</span>';
$inColor = false;
}
}

// in whitespace
elseif (in_array($char, $whitespace)) {
if ($inColor) {
$out .= '</span>';
$inColor = false;
}
}

// in "normal" text
else {
if (!$inColor) {
if ($color1) $out .= '<span class="c1">';
else $out .= '<span class="c2">';
$color1 = !$color1;
$inColor = true;
}
}
}
$out .= $char;
}
$out .= '</span>';
return $out;
}
Thanks,

- John

"John van Terheijden" <va************************@home.nl> 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 <div> tags.

// input text with all sorts of HTML tags and whitespace
$str = "<h1>Title here</h1>
<p>This is a <strong>nice</strong> <img src="picture.gif" /><br>
and some nice text </p>";

// color the words and echo
echo color_words($str);

Would like the output to be:
<h1><div class="c1">Title</div> <div class="c2">here</div></h1>
<p><div class="c1">This</div> <div class="c2">is</div> <div
class="c1">a</div> <strong><div class="c2">nice</div></strong> <img
src="picture.gif" /><br>
<div class="c1">and</div> <div class="c2">some</div> <div
class="c1">nice</div> <div class="c2">text</div> </p>

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 <tags> shouldn't
be colored. I fiddled some more with my initial for loop and came up with a
solution. [...]


But there are some bugs, the beginning and end of your teststring look
like this:

<span></span><h1><span class="c1">Title</span> ...
^^^^^^^^^^^^^

.... <span class="c2">text</span> </p></span>
^^^^^^^

I've played around a bit with regular expressions:

function color_words($string) {
$color = 2;
$pattern = '#[^\s<>]+(?![^<]*>)#e';
$replace = '"<span class=\"c".($color = 3-$color)."\">$0</span>"';
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 <span> </span> 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 .= '</span>';
$inColor = false;
}
}

// in whitespace
elseif (in_array($char, $whitespace)) {
if ($inColor) {
$out .= '</span>';
$inColor = false;
}
}

// in "normal" text
else {
if (!$inColor) {
if ($color1) $out .= '<span class="c1">';
else $out .= '<span class="c2">';
$color1 = !$color1;
$inColor = true;
}
}
}
$out .= $char;
}
if ($inColor) $out .= '</span>';
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" <ne*****@gmx.net> schreef in bericht
news:38********************************@4ax.com...
.oO(John van Terheijden)
The given solutions didn't do anything with the fact that <tags> 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:

<span></span><h1><span class="c1">Title</span> ...
^^^^^^^^^^^^^

... <span class="c2">text</span> </p></span>
^^^^^^^

I've played around a bit with regular expressions:

function color_words($string) {
$color = 2;
$pattern = '#[^\s<>]+(?![^<]*>)#e';
$replace = '"<span class=\"c".($color = 3-$color)."\">$0</span>"';
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 <span> </span> were
needed 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'll
use 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" <ne*****@gmx.net> schreef in bericht
news:la********************************@4ax.com...
.oO(John van Terheijden)
You were right about the "bugs". The opening and closing <span> </span> 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'll
use 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.