Sorting the Data
I'm going to make this a little easier. Instead of using regex to check everything, we will just
explode the string. This is going to split the line into parts, which we will separate by spaces. Here's an example of a log line.
Sorry, Lowkey.
You were the first one I saw. So, what happened here? The CHAT at the beginning of the line shows us that it was a message written into regular chat. Lowkey@forums is the one who wrote it, and the message was everything that follows. In this case, it was "hahah!!!". Let's split our lines into pieces now.
Code: Select all
#!/usr/bin/php
<?php
while (!feof(STDIN))
{
$line = rtrim( fgets(STDIN) );
$part = explode(" ", $line);
}
?>
$part is another variable. It's collecting what's returned from the explode function, which is splitting $line at every " " (space). This is going to return an array, and store that in $part. This is what $part looks like if we run it through
print_r().
Code: Select all
Array
(
[0] => CHAT
[1] => Lowkey@forums
[2] => hahah!!!
)
So, at every space, we've cut the line of text. Now, we can check pieces of it much more easily. You can read each piece by using our variable $part and the array key we want to call. 0 is the first key, so we will check to see what that is, and make sure it's a line we want to use. In this case, it's "CHAT".
Checking the Line
Let's make an
if statement, and see if it's a chat line. We're just going to see if $part[0] is equal to "CHAT".
Code: Select all
#!/usr/bin/php
<?php
while (!feof(STDIN))
{
$line = rtrim( fgets(STDIN) );
$part = explode(" ", $line);
if ($part[0] == "CHAT")
{
}
}
?>
So, if the line doesn't start with "CHAT", we're just going to ignore it and wait for the next line. This if statement only returns true if someone writes something into chat. Let's get the message they wrote now ..
Code: Select all
#!/usr/bin/php
<?php
while (!feof(STDIN))
{
//$line = rtrim( fgets(STDIN) );
$line = "CHAT Lowkey@forums hahah!!!";
$part = explode(" ", $line);
if ($part[0] == "CHAT")
{
$message = implode(" ", array_slice($part, 2));
}
}
?>
Here, we're setting $message to what the user said. We're using
implode() to put together the array being returned by
array_slice(). It's going to separate each piece by a space so it's a normal sentence now instead of an array. We're offsetting the array by 2, so it's going to get rid of the first two items, which are the type of log (CHAT) and the user (Lowkey@forums). It will leave whatever the user said.
So, here's what $message actually is, just to show you what we're dealing with a little more visually.
Code: Select all
$part = array("CHAT", "Lowkey@forums", "hahah!!!");
$message = "hahah!!!";
// If the message was longer than one word ..
$part = array("CHAT", "Lowkey@forums", "hahah!!!", "weeee");
$message = "hahah!!! weeee";
Let's now check to see if what they said was "Hello". This should be pretty straight-forward at this point.
Code: Select all
#!/usr/bin/php
<?php
while (!feof(STDIN))
{
$line = rtrim( fgets(STDIN) );
$part = explode(" ", $line);
if ($part[0] == "CHAT")
{
$message = implode(" ", array_slice($part, 2));
if (strtolower($message) == "hello")
{
}
}
}
?>
The only new thing here is
strtolower. This is so they don't have to type it a certain way to trigger the script. Also, make sure that what we're checking it to be equal to is all lowercase, otherwise making the first lowercase will always make it return false and never work.