<?php
$file = "/home/public_html/path/to/random-quotes.txt";
$quote = file($file);
echo $quote[array_rand($quote)];
?>Содержание random-quotes.txt
This is your first quote.
This is your second quote.
This is your third quote.
This is your fourth quote.
This is your fifth quote.Или:
<?php
$quote=file("your-quotes.txt");
$totalnumberofquotes=count($quote);
$rdm=rand(1,$totalnumberofquotes);
echo $quote[$rdm];
?>Или:
<?php include('testimonials.txt');
srand ((double) microtime() * 100000);
$random_number = rand(0,count($quotes)-1);
echo ($quotes[$random_number]);
?>Или:
$file= "quotes.txt";
$quotes = file($file);
srand((double)microtime()*1000000);
$randomquote = rand(0, count($quotes)-1);
echo $quotes[$randomquote];Вариант:
<?php
$quotes[] = 'Монета, упавшая на дно реки, может показаться рыбам подарком с неба. Что они будут делать с этим подарком?';
$quotes[] = 'Если увидишь нечто, к чему стоит приложить руку, поступи не так, как подсказывает разум, но как велит настроение.';
$quotes[] = 'Если то, что ты делаешь, трудно тебе - посмотри, нуждаешься ли ты в этом.';
$quotes[] = 'Возможно, твои ошибки - это то, что нужно Миру.';
$quotes[] = 'Всякий может ударить слабого, но только слабый хочет ударить слабого.';
srand ((double) microtime() * 1000000);
$random_number = rand(0,count($quotes)-1);
echo ($quotes[$random_number]);
?>Ещё:
$Arr = array(
'Hello',
'Good day!',
'Bye Bye'
);
echo $Arr[rand(0, sizeof($Arr)-1)];
echo $Arr[array_rand($Arr)];Ещё:
$hello=array('ПРИВЕТ','Пока','Добрый день');
echo $hello[rand()%3];Ещё:
$quoteList = array(
"quote one",
"quote two",
"quote three",
"quote four"
);
echo $quoteList[mt_rand(0, count($quoteList)-1)];Или:
First create a page called getquote.php
<?php
$quotes[] = "quote 1";
$quotes[] = "quote 2";
$quotes[] = "quote 3";
$quotes[] = "quote 4";
$quotes[] = "quote 5";
$quotes[] = "quote 6";
srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($quotes)-1);
echo "<p>" . $quotes[$randomquote] . "</p>";
?>
2. Add the following include to your page:
<?php include "getquote.php"; ?>That’s it… enjoy! if you have any problems let me know, should work fine as is… if you like it please “like”, ill add more if people find them useful!
Или:
<?php
/**
* This simple snippet can be used to display
* a random quote.
*/
// define an array that lists out the quotes
$quotes=array(
'Enjoy Life',
'A liar always a liar',
'Nature learns',
'Live long',
'God is truth'
);
function get_random_quote($quotes)
{
// generate a random no between 0 and size of quotes array -1
// as the index starts with 0
$random_no = rand(0,sizeof($quotes)-1);
// get the quote corresponding to random no generated
$quote = $quotes[$random_no];
return $quote;
}
/**
* Usage
*/
echo get_random_quote($quotes);
?>Или:
randomQuote.php
<?php
$quoteArray = array(
'"A journey of a thousand miles begins with a single step" - Laozi',
'"The more you sweat in training, the less you bleed in battle."',
'"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid."',
'"It is the greatest of all mistakes to do nothing because you can only do little. Do what you can." - Sydney Smith',
'"If you can solve your problem, then what is the need of worrying? If you cannot solve it, then what is the use of worrying?" - Shantideva',
'"The important thing is not to stop questioning. Curiosity has its own reason for existing." - Albert Einstein',
'"It is difficult to say what is impossible, for the dream of yesterday is the hope of today and the reality of tomorrow." - Robert H. Goddard',
'"A master in the art of living draws no sharp distinction between his work and his play." - L. P. Jacks ',
'"I cannot give you the formula for success, but I can give you the formula for failure - which is: Try to please everybody." - Herbert Bayard Swope',
'"Ninety-nine percent of the failures come from people who have the habit of making excuses" - George Washington Carver',
'"Show me a thoroughly satisfied man and I will show you a failure." - Thomas Edison',
'"All that a man achieves and all that he fails to achieve is the direct result of his own thoughts." - James Allen',
'"Doubt kills more dreams than failure ever will." - Suzy Kassem',
'"Thoughts and words are containers or weapons for carrying creative or destructive power." - Joyce Meyer',
'"Absorb what is useful, discard what is useless, and add what is essentially your own." - Bruce Lee',
'"Every day do something that will inch you closer to a better tomorrow." - Doug Firebaugh',
'"All great things are simple, and many can be expressed in single words: freedom, justice, honor, duty, mercy, hope." - Winston Churchill',
'"Darkness cannot drive out darkness; only light can do that. Hate cannot drive out hate; only love can do that." - Martin Luther King, Jr.',
'"Some people want it to happen, some wish it would happen, others make it happen." - Michael Jordan',
'"There is only one way to avoid criticism: do nothing, say nothing and be nothing." - Aristotle',
'"Talent is a pursued interest. Anything that you\'re willing to practice, you can do." - Bob Ross',
'"Great spirits have always encountered violent opposition from mediocre minds." - Albert Einstein',
'"The only true wisdom is in knowing you know nothing." - Socrates',
'"True wisdom comes to each of us when we realize how little we understand about life, ourselves, and the world around us." - Socrates',
'"We make a living by what we get. We make a life by what we give." - Winston S. Churchill', //This may not be Churchill.
'"Success always demands a greater effort." - Winston S. Churchill',
'"Don\'t cling to a mistake just because you spent a lot of time making it." - Unknown',
'"The master has failed more times than the beginner has even tried" – Stephen McCranie',
'"Never give up on a dream just because of the time it will take to accomplish it. The time will pass anyway." – Earl Nightingale',
'"A nation of well-informed men who have been taught to know and prize the rights which God has given them cannot be enslaved. It is in the region of ignorance that tyranny begins." – Benjamin Franklin',
'"Inspiration exists, but it has to find us working." – Pablo Picasso',
'"Those who never make mistakes work for those of us who do." - Henry Ford',
'"Pride is concerned with who is right. Humility is concerned with what is right." - Ezra Taft Benson',
'"Working hard for something we don\'t care about is called stress; working hard for something we love is called passion." - Simon Sinek',
'"The true test of a man\'s character is what he does when no one is watching." - John Wooden',
'"Not everything that can be counted counts, and not everything that counts can be counted." - 1963 paper by William Bruce Cameron',
'"Humility is not thinking less of yourself, it\'s thinking of yourself less." - CS Lewis',
'"It is better to be a warrior in a garden than to be a gardener in a war."',
'"A Grandmaster doesn\'t say he\'s a Grandmaster. Other people say it for him."',);
$total = count($quoteArray);
$random = (mt_rand()%$total);
$quote = "$quoteArray[$random]";
print("$quote");
?>Вывод:
<?php include("randomQuote.php"); ?>Или:
<?php
$strings = array('Quote #1','Quote #2');
echo $strings[array_rand($strings)];?>Или:
<?php
//array of short quotes (taken from http://www.curatedquotes.com/)
$array_of_quotes = array(
'I don\'t want to earn my living; I want to live.',
'Life shrinks or expands in proportion to one\'s courage.',
'Life must be lived forwards, but can only be understood backwards.',
'Be the change you wish to see in the world.',
'This is your life, and it\'s ending one minute at a time.',
'Love the life you live, and live the life you love.'
);
//array_rand(array, number)
$randome_key = array_rand($array_of_quotes);
//print out a random quote
print $array_of_quotes[$randome_key];
?>