Page 1 of 1

more complex text replaces

Posted: Thu Oct 16, 2008 9:38 am
by Grahamhart
Hi i am having a problem replacing some plain text with colored text, I found this easy when using one word but once i started allowing the input of arrays it stoped working, been debuging for hours with no luck.

My code is:

while ($row=mysql_fetch_assoc($primary))
{
extract($row);
$high = "hello world";
$include = get_include_contents("includes/'$thread'.inc");
$getval = explode(" ", $high);
$lowarray = implode(", ", $getval);
$reparray = implode("</font></b>, <b><font color='00FF00'>", $getval);
$highcont = str_replace("$lowarray", "<b><font color='00FF00'>$reparray</font></b>", $include);
$highsub = str_replace("$lowarray", "<b><font color='00FF00'>$reparray</font></b>", $subject);

print "<table width='700' border='1' bordercolor='#000000' cellpadding='0' cellspacing='0' align='center'>";
Print "<tr>";
print "<td width='500' bgcolor='000000'><font color='FFFFFF'><b>Subject:</b> $highsub</font></td><td width='200' bgcolor='000000'><font color='ffffff'><b>Posted by:</b> $author</font></td>";
print "</tr><tr>";
print "<td colspan='2'>$highcont</td>";
print "</tr><tr>";
print "<form enctype='multipart/form-data' method='post' action='viewtopic.php'><td bgcolor='000000'><font color='ffffff'><b>Date Posted:</b> $posted</font></td><td align='right' bgcolor='000000'><INPUT type=hidden name=thread VALUE='$subject'><INPUT type='image' src='images/comments.gif' alt='Read Comments' name='submit'></td></form>";
print "</tr></table>";
print "<br>";

}


from my understanding this should brak "hello world" into hello and world

Then make an array lookign like this $lowarray = (hello, world)
Then another like this $reparray = (hello</font></b>, <b><font color='00FF00'>world)

when put back into my str_replace array which has the other half the green code included should then look like

$replace = str_replace("hello, world", "<b><font color='00FF00'>hello</font></b>, <b><font color='00FF00'>world</font></b>", $input)
i dont get why once this is made up it doesnt add the coloring code to all fo my occurances, but im new to this and not sure my way of thinking is the best way to do things.

anybody got an idea?

Re: more complex text replaces

Posted: Thu Oct 16, 2008 2:40 pm
by requinix

Code: Select all

$replace = str_replace("hello, world", "<b><font color='00FF00'>hello</font></b>, <b><font color='00FF00'>world</font></b>", $input);
That looks for the string "hello, world" and replaces it with that HTML stuff. It is not looking for "hello" and "world" separately.

The syntax you want is

Code: Select all

$replace = str_replace(
    array("hello", "world"),
    array("<b><font color='00FF00'>hello</font></b>", "<b><font color='00FF00'>world</font></b>"),
    $input);
Which means you'll be undoing some of the work you did ;)

Also: when giving the color in hex you need to put a # in front:

Code: Select all

<font color='#00FF00'>
(No comment about how you shouldn't be using <font>. Not my problem.)

Re: more complex text replaces

Posted: Fri Oct 17, 2008 2:46 am
by Grahamhart
hehe font will be replaced out once i apply the style sheets, just trying to learn some php atm tho :D Thanks for the tips