Jump to content
php.lv forumi

Random teksts.


Cibiņš

Recommended Posts

$vards1 = "Jaanka";
$vards2 = "Kazha";
$vards3 = "Pacans";
$vards4 = "Vladjiks";

 

Kaa iisti lai randomaa paraada vaardus (tikai vienu vardu piem Janka, refreshojot lapu paraada Vladjiks, atkal refreshojot - Kazha utt..nu ceru domu saprataat..), gribas panaakt kaukaa vienkaarshaaku variantu..

 

Itkaa shitas skripts ir bet man vinjsh neder..jo liek kkaadaa rakaa <br /> tagu, ja novaac to \n ta vispar nekas nestrada, gribas jau lai lapaa ir defineejumi nevis velk no teksta faila.

 

 

<?php
$filename = "vardi.txt";
$file = file($filename);

 srand((double)microtime()*1000000);
 while ($RandomRotator == "") {
 $RandomRotator = ereg_replace("\n","",$file[rand(0,count($file))]);
 }
echo "$RandomRotator";
?>

 

un fails vardi.txt kur ir tie vaardi iekshaa

 

Janka
Kazha
Pacans
Vladjiks

Edited by Cibiņš
Link to comment
Share on other sites

$names = array("loļiks", "petķa", "fedja", "lunis");
echo $names[rand(0,count($names)];

 

vai ja no faila:

$names = file('fails.txt');
echo trim($names[rand(0,count($names))]);

Edited by briedis
Link to comment
Share on other sites

$names = array("loļiks", "petķa", "fedja", "lunis");
echo $names[rand(0,count($names)];

 

Paldies par paliidziibu bet raada Internal Server Error konkreeti uz sho failu taatad kkas nau te pareizi :p

 

Reku ir tas ko vajadzeeja :p

 

$input = array("Pacans", "Vladjiks", "Urla", "Dumaj", "Vii");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]];

Edited by Cibiņš
Link to comment
Share on other sites

Te bija pazudusi aizverošā iekava:

$names = array("loļiks", "petķa", "fedja", "lunis");

echo $names[rand(0,count($names))];

 

Kas tev liekas efektīvāk - samaisīt visus masīva elementus (tas nozīmē cikls, vai pat vairāki), vai paņemt vienu elementu pēc random indeksa (atliek izdomāt vienu ciparu uz dullu, ņemot vērā kopējo elemetu skaitu)?

 

Nevermind, palasīju manuāli...

Edited by briedis
Link to comment
Share on other sites

Cibiņš: kādā jēgā vajag izvelēties divas masīva atslēgas, bet jāizmanto tikai viena?

 

$input = array("Pacans", "Vladjiks", "Urla", "Dumaj", "Vii");
echo $input[array_rand($input)];

 

LIELS paldies bubu :):):) Nu jaa uz Localhosta mans variants gaaja bet uz servera nee..bet Tavs variants straadaa perfekti :)) Lidz ar to problema atrisinaata :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...