dslreports logo
site
 
    All Forums Hot Topics Gallery
spc

spacer




how-to block ads


Search Topic:
uniqs
1065
share rss forum feed

cmhbob
Did...Did I Do That?
Premium
join:2001-03-13
Muskogee, OK

Random Quote Generator

I 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/

--
Bob
More about me


sbconslt

join:2009-07-28
Los Angeles, CA
Because the line numbers are present in the quotes.txt file:

»genealogy.ravensbeak.com/quotes.txt

There'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.
--
Scott Brown Consulting


Screavics
Premium
join:2011-06-23
Pearcy, AR
reply 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


sbconslt

join:2009-07-28
Los Angeles, CA
The one-time fix of the content is a much better choice than committing a special workaround to the code.


Screavics
Premium
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;
?>
 


sbconslt

join:2009-07-28
Los Angeles, CA
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
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


sbconslt

join:2009-07-28
Los Angeles, CA

1 recommendation

cat quotes.txt | cut -d" " -f2- > fixed.txt
 


cdru
Go Colts
Premium,MVM
join:2003-05-14
Fort Wayne, IN
kudos:7
said by sbconslt:

[code]
cat quotes.txt | cut -d" " -f2- > fixed.txt
[/code]

Sometimes the simplest answer is...the simplest answer.

cmhbob
Did...Did I Do That?
Premium
join:2001-03-13
Muskogee, OK
reply to cmhbob
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.
--
Bob
More about me


cowboyro
Premium
join:2000-10-11
Shelton, CT
reply to cmhbob
You may want to resize the tiny pictures on the main page... 1MB for 100x175 is ridiculous...