473,396 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String conversion algorithm

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
8 2168
"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
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
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
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
.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
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
.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Zombie | last post by:
Hi, what is the correct way of converting contents of a <string> to lowercase? There are no methods of <string> class to do this so I fallback on strlwr(). But the c_str() method returns a const...
1
by: kaede | last post by:
Hi all, Given the following data format: { "(1,2), "(2.5, 3.5)", .... } I would like to define a vector<string> to stored this data: v = "(1,2)", v = "(2.5, 3.5"), .... and would like to...
4
by: oddstray | last post by:
Hi, I have a number which is larger than the max unsigned long int. I don't have 64-bit integers available to me. I need to get the resulting 40-bit hex string. I can't find any algorithm...
8
by: 116Rohan | last post by:
I came across a question in one of the Computing olympiad regarding string pattern matching. Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the...
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
29
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to...
11
by: Sudzzz | last post by:
Hi, I'm trying to convert a string something like this "{201,23,240,56,23,45,34,23}" into an array in C++ Please help. Thanks, Sudzzz
6
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.