473,836 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.gi f" /><br>
and some nice text </p>";

// color the words and echo
echo color_words($st r);

Would like the output to be:
<h1><div class="c1">Titl e</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.gi f" /><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 2191
"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.gi f" /><br>
and some nice text </p>";

// color the words and echo
echo color_words($st r);

Would like the output to be:
<h1><div class="c1">Titl e</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.gi f" /><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>eve nword1</...>
<oddcolor>oddwo rd1</...> <evencolor></...> <oddcolor>evenw ord2</...>
<evencolor>oddw ord2</...>"
but we need this result: "<evencolor>eve nword1</...>
<oddcolor>oddwo rd1</...> <evencolor>even word2</...>
<oddcolor>oddwo rd2</...>"

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.supe rnews.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.gi f" /><br>
> and some nice text </p>";
>
> // color the words and echo
> echo color_words($st r);
>
> Would like the output to be:
> <h1><div class="c1">Titl e</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.gi f" /><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>".$wor d."</color1>"; // colorize it
else $word = "<color2>".$wor d."</color2>";
$i++; // increase word
counter
}
$dst .= $word.substr($t mp, $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: ".htmlspecialch ars($src)."<br> ";
echo "destinatio n: ".htmlspecialch ars($dst)."<br> ";
echo "we have ".$i." words";
echo "</pre>";
[/PHP]
"John van Terheijden" <va************ ************@ho me.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.gi f" /><br>
and some nice text </p>";

// color the words and echo
echo color_words($st r);

Would like the output to be:
<h1><div class="c1">Titl e</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.gi f" /><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************ ************@ho me.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.gi f" /><br>
and some nice text </p>";

// color the words and echo
echo color_words($st r);

Would like the output to be:
<h1><div class="c1">Titl e</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.gi f" /><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">Titl e</span> ...
^^^^^^^^^^^^^

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

I've played around a bit with regular expressions:

function color_words($st ring) {
$color = 2;
$pattern = '#[^\s<>]+(?![^<]*>)#e';
$replace = '"<span class=\"c".($co lor = 3-$color)."\">$0</span>"';
return preg_replace($p attern, $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.ne t> schreef in bericht
news:38******** *************** *********@4ax.c om...
.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">Titl e</span> ...
^^^^^^^^^^^^^

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

I've played around a bit with regular expressions:

function color_words($st ring) {
$color = 2;
$pattern = '#[^\s<>]+(?![^<]*>)#e';
$replace = '"<span class=\"c".($co lor = 3-$color)."\">$0</span>"';
return preg_replace($p attern, $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.ne t> schreef in bericht
news:la******** *************** *********@4ax.c om...
.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
7385
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 pointer which cannot be used with strlwr() as it does the conversion inplace. So, I use the following logic of copying the contents to a dynamically allocated char* array and then doing the conversion: -----------------------------
1
2251
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 extract the integer/double string value to real int/double and use it in computation. What is the best way to do it?
4
5913
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 or solution when I search the web. I've seen a couple of suggestions searching Usenet, but neither of them works. One tried to use a union, and the other tried to convert each byte of the float.
8
3913
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 denominator, that prints out the decimal representation. If the decimal representation has a repeating sequence of digits, it should be indicated by enclosing it in brackets. For example, 1/3 = .33333333...is denoted as .(3), and 41/333 =...
12
3235
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 without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
5
5975
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 compression string ->(Unicode Conversion) byte -(Compression + Unicode Conversion) string
29
23238
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 write to the file: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) I don't know what I am doing wrong. I have posted my entire program
11
5068
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
4355
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 computation 2) compare the string binary data to get the similarity or dissimilarity result. The problem is, i already done with the image (jpg) conversion to binary and also try the algorithm structure in C# language, but i having a problem to...
0
9674
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10860
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10604
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10261
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6984
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5659
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.