PHP Help - Explode and Arrays

guerilla

All we do is win
Aug 18, 2007
11,426
428
0
No
I have a text list of html list items.

Code:
<li><a href="hxxp://www.www.ww">WWW333</a></li>
<li><a href="hxxp://yyy.yyy.yy">YYY222</a></li>
....
It is about 60 items. I want to pull and echo 15 out randomly, but would like to seed the rand function with the numerical week of the year ('W').

Any help would be appreciated. I've spent 2 hours staring at this and no further than when I started.

TIA. +Rep for helpful posts.
 


I have a robust function to do this that i wrote sadly its not documented now so i am not going to post it right now, but i promise to put it up on my blog as soon as i do.


as for your question

Code:
[FONT=monospace]$a=range(0,10);

$old_seed=rand();
srand(date('W',strtotime("+3 days")));
shuffle($a);

echo "<pre>I cange weekly:\n";
print_r($a);
echo "</pre>";


srand($old_seed);
shuffle($a);

echo "<pre>I Change all the time:\n";
print_r($a);
echo "</pre>";

[/FONT]
 
I guess i undestimated the scope of your question, here is an example with the urls in urls.txt

Code:
$a=explode("\n",file_get_contents('urls.txt'));

$old_seed=rand();
srand(date('W',strtotime("+3 days")));
shuffle($a);

$num_links_to_show = 3;
foreach($a as $ii=>$link){
  if(strlen($link) < 10)
    continue;
  if($ii == $num_links_to_show)
    break;

  echo "<li>" . trim($link) . "</li>";
}

srand($old_seed);
 
Victory, I'm sure there's a good reason, but why +3 days instead of time()? I'm guessing it's to deal with some sort of special case scenario, since right now both yield 33 on my system.

+rep, mainly for that plugin you posted the other day, but this is good too
 
Victory, I'm sure there's a good reason, but why +3 days instead of time()? I'm guessing it's to deal with some sort of special case scenario, since right now both yield 33 on my system.

+rep, mainly for that plugin you posted the other day, but this is good too

That +3 was more of a "hint" on how you can make it so all your blog pages don't update at the same time.

here is another hint, just a code snippet from the seed function i made, but didn't document so am not currently posting.


Code:
function conical_uri(){
   // TODO: clean up redundant  code
  $r=preg_replace("/index.php$/","",$_SERVER['REQUEST_URI']);
  return $_SERVER['HTTP_HOST'] . 
    preg_replace('/(index.php)?(#|\?).*$/','',$r);
}

function ord_sum($v,$w){
  $v+=ord($w);
  return $v;
}

function prand($per_page=True){
 /* stuff */
  if($per_page){    
    $mod=array_reduce(str_split(md5(conical_uri())),
              'ord_sum',
              0);
  }else{
    $mod=array_reduce(str_split(md5($_SERVER['HTTP_HOST'])),
              'ord_sum',
              0);
  }
  $seed+=$mod;
  /* more stuf */

  return $seed;

}

the full prand, will give "periodic random numbers", by converting the pages URL to a number, we can change the seed page wide, or site wide.

thanks for the rep! I don't know what it does, but it sounds good :D