[PHP] rand & eregi

Newbie Spellweaver
Joined
Aug 17, 2007
Messages
45
Reaction score
0
Hello, i'm writing a script with my friend for a sort of chatbot.

now we have this piece of code:
PHP:
                if(eregi('snap',$msg) && eregi('niet',$msg)) { 
                    $array = array("snap1", "snap2", "snap3", "snap4", "snap5");
                    $msg = array_rand($array, 5);
                }

Now what it needs to do is: If it finds the word "snap" and the word "niet" in the send message, it will pick a randomly message (snap1, snap2, snap3, snap4 or snap5) and give that to variable $msg
(later on in the script it will search for $msg in my database)

But i keep getting my error message (the message when a word isn't in the database), how can i fix this?? =]
 
re: [PHP] rand & eregi

Thats the point, it doesn't have an error message, it just doesn't get to the point where it assigns a random string to $msg
 
re: [PHP] rand & eregi

Okay first i got this:
PHP:
 $msg = strip_tags(strtolower(base64_decode($_POST['msg'])));
Which will save the message send by the person (this code is okay, i got it from the maker of the program to handle the stuff)

Then
PHP:
$msg = mysql_real_escape_string ($msg);
Looks if poeple are not trying to do a mysql injection

After that the random part comes:
PHP:
                if(eregi('snap',$msg) && eregi('niet',$msg)) { 
                    $array = array("snap1", "snap2", "snap3", "snap4", "snap5");
                    $msg = array_rand($array, 5);
                }

Then it gets $msg out of the database:
PHP:
$query = "SELECT * FROM `msg` WHERE `bericht` = '" . $msg . "'";
        $res = mysql_query($query);
        $nummers = mysql_num_rows ($res);
            if ($nummers != "0") {
                while($rows = mysql_fetch_row($res)){
                    $antwoord = $rows[1];
                }[
(not sure how this script works, someone else gave it me)

Then we get a default message if it can't find the word in the database:
PHP:
if (empty($antwoord)) {
                      $antwoord = "Ik snap u niet '" . $nick . "'. Mijn database is nog niet goed gevuld :P
Ik zoek namelijk nog scripters die mijn AI kunnen coderen. 
Interesse? Mail [email protected]!";
            }

And finaly it will send out a response to the person who talked to me
Code:
<Response>
<Message>
<? 
echo $antwoord; 
?>
</Message>
</Response>
(this part is also good becuase i got it from the maker of the program)
 
re: [PHP] rand & eregi

Hello, i'm writing a script with my friend for a sort of chatbot.

now we have this piece of code:
PHP:
                if(eregi('snap',$msg) && eregi('niet',$msg)) { 
                    $array = array("snap1", "snap2", "snap3", "snap4", "snap5");
                    $msg = array_rand($array, 5);
                }

Now what it needs to do is: If it finds the word "snap" and the word "niet" in the send message, it will pick a randomly message (snap1, snap2, snap3, snap4 or snap5) and give that to variable $msg
(later on in the script it will search for $msg in my database)

But i keep getting my error message (the message when a word isn't in the database), how can i fix this?? =]

PHP:
$msg=$array[rand(0,(count($array)))]
try this instead
 
Back