Forum Moderators: coopster

Message Too Old, No Replies

Insert array into str_replace

         

internetheaven

9:05 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know if it is possible but if it is, could somebody lend me a hand getting this to work?

$a[filename] = strtolower($a[term]);

$a[example] = str_replace(",", ' + ', $row[example_string]);
$a[example2] = explode(" + ", $a[example]);
shuffle($a[example2]);
$a[example3] = implode(" + ", $a[example2]);
$a[example4] = str_replace(" + ", ' </a> <a href=\"/folder/$a[filename]\"> ', $a[example3]);

I hope it makes sense what I'm trying to do. Everything else works, I've checked the output for every line up until the last one - it just won't put out the link ...

baze22

10:40 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



Not sure what you were trying to do. Would help if you explained the input and output you were expecting. I added some input for the undefined vars in your sample and ran your code. Based on that I am assuming that $row[example_string] will have words seperated by commas and you want each word to be a link as defined by filename? I would have used a loop after the shuffle, but using your example here's what I came up with.
<?
$a[term] = 'widget';
$row[example_string] = 'red,blue,green';

$a[filename] = strtolower($a[term]);

$a[example] = str_replace(",", ' + ', $row[example_string]);
$a[example2] = explode(" + ", $a[example]);
shuffle($a[example2]);
$a[example3] = implode(" + ", $a[example2]);
$a[example4] = sprintf('<a href="/folder/%s">%s </a>',$a[filename],str_replace(" + ", " </a> <a href=\"/folder/$a[filename]\"> ", $a[example3]));
echo "<pre>";
print_r($a);
?>

baze

baze22

10:50 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



Here's an example using the loop:
<?
$a[term] = 'widget';
$row[example_string] = 'red,blue,green';

$a[filename] = '/folder/' . strtolower($a[term]);

$a[example] = str_replace(",", ' + ', $row[example_string]);
$a[example2] = explode(" + ", $a[example]);
shuffle($a[example2]);
foreach ($a[example2] as $key=>$val) {
$a[example4] .= sprintf('<a href="%s">%s </a> ',$a[filename],$val);
}
echo "<pre>";
print_r($a);
?>

Also unless you are expecting '+'s in your string, you could skip the ',' replace and just explode using ',' right into $a[example2].

baze

Robber

10:59 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



I'll admit I haven;t really had a look at what is supposed to be going so baze may have cracked it for you, but if its this line you're having problems with:

$a[example4] = str_replace(" + ", ' </a> <a href=\"/folder/$a[filename]\"> ', $a[example3]);

Then I am guessing you get something except the link doesn't have a url in the href attribute. If this is the case its because you have used single quotes around the second argument in the str_replace() function. There is no variable interpolation when using single quotes, or in other words it wont get the value of $a[filename], it will just print out $a[filename] as a string of text.

You could therefore try changing it to:

$a[example4] = str_replace(" + ", ' </a> <a href="/folder/'.$a[filename].'"> ', $a[example3]);

baze22

11:05 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



There is no variable interpolation when using single quotes, or in other words it wont get the value of $a[filename], it will just print out $a[filename] as a string of text.

You could therefore try changing it to:

$a[example4] = str_replace(" + ", ' </a> <a href="/folder/'.$a[filename].'"> ', $a[example3]);

Thanks. Meant to mention that, but I got so wrapped up in the rest I just fixed it and forgot about it. :)

baze

Salsa

12:03 am on Dec 3, 2004 (gmt 0)

10+ Year Member



This is a little OT, but consider calling srand(), like srand(mktime()), before shuffle() in order to seed the randomizer.

baze22

12:14 am on Dec 3, 2004 (gmt 0)

10+ Year Member



This is a little OT, but consider calling srand(), like srand(mktime()), before shuffle() in order to seed the randomizer.

True only if your version is prior to 4.2.0. From the docs:

Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.

baze

Salsa

12:42 am on Dec 3, 2004 (gmt 0)

10+ Year Member



Cool! Another opportunity to eliminate a few lines of code! Does that go for functions like rand(), too? I always wondered why the features of srand() weren't just built into the various "randomizing" functions. The last time I tried rand() without seeding it with srand(), when I first started out, it totally sucked.

baze22

2:42 am on Dec 3, 2004 (gmt 0)

10+ Year Member



Does that go for functions like rand(), too? I always wondered why the features of srand() weren't just built into the various "randomizing" functions.

It shows the same note for rand().

baze