Help with a PHP script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • \(a\)

    Help with a PHP script

    I've got a cgi script that allows me to add the output of a php script to an
    html page using the following line of code.

    #insert : http://www.url.com/sript.php#

    What I'm wondering is if someone could write me a couple of lines of code
    for script.php that would randomly select one line of information from a
    text file called text.txt.

    This way, if I had the following line in my html file, the place where
    #insert : http://www.url.com/sript.php# is located, would be replaced by a
    random line of text from text.txt.

    <a href="http://www.yahoo.com"> #insert : http://www.url.com/sript.php#</a>

    The contents of text.txt would be something like this:

    This is yahoo
    yahoo is a search engine
    search using yahoo

    Thanks for the help.


  • Andy Jeffries

    #2
    Re: Help with a PHP script

    On Thu, 11 Sep 2003 06:32:04 +0000, (a) wrote:[color=blue]
    > I've got a cgi script that allows me to add the output of a php script to an
    > html page using the following line of code.
    >
    > #insert : http://www.url.com/sript.php#
    >
    > What I'm wondering is if someone could write me a couple of lines of code
    > for script.php that would randomly select one line of information from a
    > text file called text.txt.[/color]

    Assuming your text.txt file isn't too big and is in the same directory as
    /script.php :

    <?php

    // Slurp all the lines of test.txt in to an array
    $data = file("test.txt" );

    // Print a random (rand) line from 0 (start of the array) to
    // the end of the array (count($array)-1)
    print $data[rand(0, count($data)-1)];

    ?>

    If this was a homework assignment, I'll kill you in your own kitchen!!!
    :-)

    Cheers,



    Andy

    Comment

    • \(a\)

      #3
      Re: Help with a PHP script

      Nope, not homework, would never cheat.

      Just I'm not a programmer and it was faster to ask a question in an expert
      forum then to have to learn php to solve a single problem I was having.


      "Andy Jeffries" <news@andyjeffr ies.remove.co.u k> wrote in message
      news:pan.2003.0 9.11.13.16.38.9 28918@andyjeffr ies.remove.co.u k...[color=blue]
      > On Thu, 11 Sep 2003 06:32:04 +0000, (a) wrote:[color=green]
      > > I've got a cgi script that allows me to add the output of a php script[/color][/color]
      to an[color=blue][color=green]
      > > html page using the following line of code.
      > >
      > > #insert : http://www.url.com/sript.php#
      > >
      > > What I'm wondering is if someone could write me a couple of lines of[/color][/color]
      code[color=blue][color=green]
      > > for script.php that would randomly select one line of information from a
      > > text file called text.txt.[/color]
      >
      > Assuming your text.txt file isn't too big and is in the same directory as
      > /script.php :
      >
      > <?php
      >
      > // Slurp all the lines of test.txt in to an array
      > $data = file("test.txt" );
      >
      > // Print a random (rand) line from 0 (start of the array) to
      > // the end of the array (count($array)-1)
      > print $data[rand(0, count($data)-1)];
      >
      > ?>
      >
      > If this was a homework assignment, I'll kill you in your own kitchen!!!
      > :-)
      >
      > Cheers,
      >
      >
      >
      > Andy[/color]


      Comment

      Working...