...

View Full Version : str_replace not working properly



harlequin2k5
08-25-2007, 02:22 AM
what I'm trying to do is have "keywords" within content changed from text to links

right now only one post is displaying and no keywords have been changed


<?php
error_reporting(E_ALL);

require_once("functionlist.php");

$conn=doconnect();
$query = "SELECT * from keywords,affiliates where keywords.keyword=affiliates.keyword";
$result = mysql_query($query) or die("Error: " . mysql_error());
$row=mysql_fetch_array($result);
$keyword = array();


$i = 0; //i is going to be the index of our row
while ($row=mysql_fetch_array($result))
{
$keyword[$i][0]=$row['keyword'];
$keyword[$i][1]=$row['link'];
$i++; //go to the next row
}
$keywordCount = $i;

$query = "SELECT * from posts order by id";
$result = mysql_query($query) or die("Error: " . mysql_error());
$row=mysql_fetch_array($result);
echo "<div style=\"padding:10px;\">";
while ($row = mysql_fetch_array($result))
{
echo "<h1>";
echo $row['title'];
echo "</h1><p>";

//Replace content keywords with random affiliate links
$string=$row['content'];

for($r = 0; $r < $keywordCount; $r++)
$string=str_replace("$keyword[$r][0]","<a href=\"$keyword[$r][1]\" class=\"affiliatelink\">$keyword[$r][0]</a>",$string);
echo $string;
echo "</p>";
}
echo "</div>";
?>


normally I would say that I'm sure I'm missing something stupid but this is my first time with an array so I'm utterly clueless

any help is greatly appreciated!

Fumigator
08-25-2007, 05:11 AM
$string=str_replace("$keyword[$r][0]","<a href=\"$keyword[$r][1]\" class=\"affiliatelink\">$keyword[$r][0]</a>",$string);

Those array variables won't be parsed properly, you must enclose them in {}'s or (better yet) don't put them inside quotes at all.


$newString = "<a href=\"".$keyword[$r][1]."\" class=\"affiliatelink\">".$keyword[$r][0]."</a>";
$string = str_replace($keyword[$r][0], $newString, $string);

harlequin2k5
08-25-2007, 12:13 PM
thank you fumi - that worked quite well...I thought it might have something to do with the quotes but like I said I don't know anything about arrays - between you and oracleguy I now have a much better understanding of how they work

with that said...would you be able to tell me why only 1 record from "posts" is displaying?

meth
08-25-2007, 01:04 PM
You're on the right track; just got to keep bashing ur head on array's till it sinks in.

Try this version which uses a multidim array instead (untested):-




error_reporting(E_ALL);

require_once("functionlist.php");

$conn=doconnect();
$query = "SELECT * from keywords,affiliates where keywords.keyword=affiliates.keyword";
$result = mysql_query($query) or die("Error: " . mysql_error());
$row=mysql_fetch_array($result);
$keyword = array();

while ($row=mysql_fetch_array($result))
{
$keyword[] = array($row['keyword'],$row['link']);
}
$keywordCount = count($keyword);

$query = "SELECT * from posts order by id";
$result = mysql_query($query) or die("Error: " . mysql_error());
$row=mysql_fetch_array($result);
echo "<div style=\"padding:10px;\">";
while ($row = mysql_fetch_array($result))
{
echo "<h1>";
echo $row['title'];
echo "</h1><p>";

//Replace content keywords with random affiliate links
$string=$row['content'];
foreach($keyword as $k => $v)
{
$string = str_replace( $v[0],'<a href="' . $keyword[$v][1] .'" class="affiliatelink">' . $keyword[$v][0] . '</a>', $string );
}
echo $string;
echo "</p>";
}
echo "</div>";

harlequin2k5
08-25-2007, 03:24 PM
You're on the right track; just got to keep bashing ur head on array's till it sinks in.

yep there's a hole in the wall *chuckles*

you're suggestion did not work and believe it or not I was almost close to what you provided when I had started on this - and I believe what you've provided will make it easier for me to figure out how to randomize the affiliate link ;) unfortunately I'm not yet familiar with all of these new errors I've been exposed to :(

the error generated is:

Warning: Illegal offset type

fumi and meth have been such great help; as with the many times that aero has helped me I feel like I'm actually learning rather than have a piece of code thrown at me - thanks to you both!

meth
08-25-2007, 04:22 PM
Gah, spotted what I'd say is the error:




foreach($keyword as $k => $v)
{
$string = str_replace( $v[0],'<a href="' . $keyword[$v][1] .'" class="affiliatelink">' . $keyword[$v][0] . '</a>', $string );
}


Should be:



foreach($keyword as $k => $v)
{
$string = str_replace( $v[0],'<a href="' . [$v][1] .'" class="affiliatelink">' . [$v][0] . '</a>', $string );
}


Sh!t happens when you cut corners with cntrl c, cntrl v :(

harlequin2k5
08-25-2007, 04:35 PM
I'm starting to think I'm not the only one that needs some coffee lol

now I get an unexpected [ error

edit: and if I can ask a stupid question? where does $v get declared?

meth
08-25-2007, 04:40 PM
To randomize the link, you can use a php array function to select randomly from the $keyword array instead of a foreach loop.

array_rand() (http://www.php.net/manual/en/function.array-rand.php) will select a random key of the array.

If you dont want to have the same link appearing more than once, you can delete the key from the array straignt after using it: unset($keyword[$v]);

For eg:



echo "<div style=\"padding:10px;\">";
while ($row = mysql_fetch_array($result))
{
echo "<h1>";
echo $row['title'];
echo "</h1><p>";

//Replace content keywords with random affiliate links
$string=$row['content'];
if( is_array($keyword) && !empty($keyword) ) {
$key = array_rand($keyword);
$string = str_replace( $keyword[$key][0],'<a href="' . $keyword[$key][1] .'" class="affiliatelink">' . $keyword[$key][0] . '</a>', $string );
//optional unset the link for unique links only
unset($keyword[$key]);
//end option
}
//unset
echo $string;
echo "</p>";
}

echo "</div>";

meth
08-25-2007, 04:44 PM
$v is declared in the foreach loop condition ($keyword as $k => $v). Whats the error?

harlequin2k5
08-25-2007, 04:48 PM
Parse error: syntax error, unexpected '['

harlequin2k5
08-25-2007, 04:51 PM
ok I think I'm over this...I've gotten lost again and now nothing is displaying
edit: I must have copied something over wrong - it's working

and of course I've left my book at work lol

thanks for your help meth - I was so close but my brain has fogged over again

edit: but it's still displaying only 1 record from posts - there are actually 2 in the table

meth
08-25-2007, 05:17 PM
Use a do while instead of a while for your resultset then. Check out the commented out code for an eg of it working:



/*
//here's a dummy run with some array data instead of sql results
//comment out your code and uncomment this code to see it work.
$keyword[] = array('quick','www.quick.com');
$keyword[] = array('brown','www.brown.com');
$keyword[] = array('fox','www.fox.com');
$keyword[] = array('jumped','www.jumped.com');
$keyword[] = array('over','www.over.com');
$keyword[] = array('lazy','www.lazy.com');
$keyword[] = array('dog','www.dog.com');

$string[] = 'The quick brown fox is on holidays. Please leave a message.';
$string[] = 'The lazy dog could not be bothered; the fox can go jump.';
$string[] = 'The quick fox went splat and the dog got over being lazy.';
$string[] = 'The fox aint so quick any more and the dog married a fat brown cat.';

echo '<div>';

foreach($string as $k => $v)
{
echo "<h1>";
echo 'Eg Title';
echo "</h1><p>";

//Replace content keywords with random affiliate links
$string = $v;
foreach( $keyword as $k2 => $v2 ) {
$string = str_replace( $v2[0],'<a href="' . $v2[1] .'" class="affiliatelink">' . $v2[0] . '</a>', $string );
}
//unset
echo $string;
echo "</p>";
}

echo "</div>";

*/

error_reporting(E_ALL);

require_once("functionlist.php");

$query = "SELECT * from keywords,affiliates where keywords.keyword=affiliates.keyword";
$result = mysql_query($query) or die("Error: " . mysql_error());
$row = mysql_fetch_array($result);
$keyword = array();

while ($row=mysql_fetch_array($result))
{
$keyword[] = array($row['keyword'],$row['link']);
}

$keywordCount = count($keyword);

$query = "SELECT * from posts order by id";
$result = mysql_query($query) or die("Error: " . mysql_error());
$row=mysql_fetch_array($result);

echo "<div style=\"padding:10px;\">";
do {
echo "<h1>";
echo $row['title'];
echo "</h1><p>";

//Replace content keywords with random affiliate links
$string=$row['content'];

foreach($keyword as $k => $v)
{
$string = str_replace( $v[0],'<a href="' . $v[1] .'" class="affiliatelink">' . $v[0] . '</a>', $string );
}

echo $string;
echo "</p>";

} while ( $row = mysql_fetch_array($result) );

echo "</div>";

harlequin2k5
08-25-2007, 05:58 PM
omg - I haven't tried it yet but that thought had entered my mind as I was trying to figure out how to correct the while statement - I knew that's where the hang up was!

so simple - I will edit this post after I've tried it

harlequin2k5
08-26-2007, 02:24 AM
meth!!

I still have quite a bit of studying to do with the solution you've provided but after stepping away from the computer for a few hours and not thinking about it, I was finally able to mostly understand your quick fox example and then I copied in your code...it works beautifully!

thank you for your patience and your help!!!

edit: I meant to show you the link... http://shiznit.no-ip.info/coasttocoast - there's still alot more work to be done with the content for this site but at least we're off to a great start! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum