random item from variable in array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

random item from variable in array

Post by mesz »

OK...I'm still building the same thing I've been banging on about on here for weeks now, and I have another problem.
(It is on an IIS server using Integrated Windows Authentication so I can't show anyone unfortunately).
Anyway, I have previously been using that random image script - you know the one, where there is a folder of images for it to select from -
but now I want to randomise the image using a variable from a flat file.
But it ain't working.

The script I am using is -

Code: Select all

<?php
srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($pieces[5])-1);
$data = file('org/BLAH/news.txt'); 
$data = array_reverse($data); 
foreach($data as $element) { 
    $element = trim($element); 
    $pieces = explode("¬", $element); 
    $pieces[4] = preg_replace('!\[url=(.+?)\](.+?)\[/url\]!Uim', '<a href="\1" target=\"_blank\">\2</a>', $pieces[4]); 
    $pieces[5] = preg_replace('!\[img\](.+?)\[/img\]!', '<img src="\1">',$pieces[5]);
echo $pieces[5][$randomquote];
}?>
It just prints

Code: Select all

< <
to screen.

Any clues....
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

without seeing the input we couldn't possibly tell you the exact cause.

However, if you are looking to include a random image link from a flat file, consider the following:

Do you really need to use bbcode in the file? Why not just use html anchor tags? (<a href= ... )

Are these directories stored on your server? If so, there is no need to store links in a flat file, you can random pic the image straight from the directory.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

the files aren't stored on my server, but rather the variable $pieces[5] is a url to a google thumbnail - or similar
(this project has grown organically into an unplanned big mess, I know).
the bbcode has to be there, because it helps at a later stage for re-using the variable in other displays and xml feeds.
this is an entry from the flat file

Code: Select all

28.11.05¬name¬name@name.com¬this is a subject title¬¬[img]http://images.google.co.uk/images?q=tbn:dkmEd38mwuwJ:upload.wikimedia.org/wikipedia/lt/thumb/3/3f/Mandrill.jpg/230px-Mandrill.jpg[/img]¬text and information goes here.blah, blah, blah, etc.<BR></BR>
Post Reply