cmhbobDid...Did I Do That? Premium Member join:2001-03-13 Fort Gibson, OK Motorola MB7220
|
cmhbob
Premium Member
2012-Jun-6 3:27 pm
Random Quote GeneratorI grabbed some PHP for a random quote generator from hashbangcode. Nice little snippet. Grabs a random number. Reads a text file to that line number, and pulls the appropriate text into a variable. But it's also echoing the line number of the text, I'm assuming from where it gets the text. How can I fix that? $file= "quotes.txt"; $quotes = file($file); srand((double)microtime()*1000000); $randomquote = rand(0, count($quotes)-1); echo $quotes[$randomquote]; To see it in action, see » genealogy.ravensbeak.com/ |
|
|
Because the line numbers are present in the quotes.txt file: » genealogy.ravensbeak.com ··· otes.txtThere's nothing wrong with your code, it's doing exactly what you you told it to. All you have to do is fix the file, it doesn't need to be numbered like that to work. |
|
Screavics Premium Member join:2011-06-23 Pearcy, AR |
to cmhbob
You can do preg_replace. $file= "quotes.txt";
$quotes = file($file);
srand((double)microtime()*1000000);
$randomquote = rand(0, count($quotes)-1);
$quotes = preg_replace('/[^\\/\-a-z\s]/i, ' ', $quotes);
echo $quotes[$randomquote];
{EDIT} fixed the preg_replace |
|
|
The one-time fix of the content is a much better choice than committing a special workaround to the code. |
|
Screavics Premium Member join:2011-06-23 Pearcy, AR |
That maybe true but I'm just delivering what the fellow board member asked for. Maybe they have a much larger file than what we are seeing. Who knows the real purpose, they asked for help and I am delivering. Here is a better solution to my preg_replace it finds the first space which is right after the number and starts the quote from there and returns the rest. Effectively removing the number without stripping anything else. <?php
$file= "quotes.txt";
$quotes = file($file);
srand((double)microtime()*1000000);
$randomquote = rand(0, count($quotes)-1);
//returns the length of the quote
$len = strlen($quotes[$randomquote]);
//finds the first occurrence of space
$first = stripos($quotes[$randomquote], " ");
//starts at the first space and continues on the rest of the quote stripping the line number
$quote = substr($quotes[$randomquote], $first, $len);
echo $quote;
?>
|
|
|
If the file was too large to fix manually you would still be better served fixing it one-time via a throwaway script, rather than jacking up the permanent presentation code. A legally blind person can look at your version versus the starting point and tell which is wiser engineering. |
|
Screavics Premium Member join:2011-06-23 Pearcy, AR |
Ok that's your opinion and those of some others but like I said, OP asked, and I presented a solution. This solution is a solution to remove line numbers from a file with line numbers and opens the file (quotes.txt) and then writes it to (quote.txt). That code can be modified as you see fit and you can choose either solution you may want. <?php
$file = "./quotes.txt";
//opens source file
$quotes_file = file($file);
//opens target file
$fh = fopen("quote.txt", 'w');
//proceeds through each line
foreach ($quotes_file as $line_num => $line) {
//get the length of line
$len = strlen($line);
//finds the first occurrence of space
$first = stripos($line, " ");
//starts at the first space and continues on the rest of the quote stripping the line number
$quote = substr($line, $first+1, $len);
//write new content to file
fwrite($fh, $quote);
}
//close file
fclose($fh);
?>
Does that satisfy you sbconslt even though this topic wasn't about you. Being "spiteful" towards others isn't a solution to a problem presented and critiquing others to the point of insults doesn't help your solution either. " A legally blind person can look at your version versus the starting point and tell which is wiser engineering." ---> oh wow adding a whopping few extra lines to solve the problem |
|
1 recommendation |
cat quotes.txt | cut -d" " -f2- > fixed.txt
|
|
cdruGo Colts MVM join:2003-05-14 Fort Wayne, IN |
cdru
MVM
2012-Jun-6 9:35 pm
said by sbconslt:[code] cat quotes.txt | cut -d" " -f2- > fixed.txt [/code] Sometimes the simplest answer is...the simplest answer. |
|
cmhbobDid...Did I Do That? Premium Member join:2001-03-13 Fort Gibson, OK |
cmhbob
Premium Member
2012-Jun-6 11:41 pm
Well. Crap. I've uploaded a newer file since that first file went up. Wonder where it is?
Thanks all. Sometimes it's so obvious that you can't see it. |
|
|
to cmhbob
You may want to resize the tiny pictures on the main page... 1MB for 100x175 is ridiculous... |
|