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'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
Comment