sedvis Posted March 15, 2012 Share Posted March 15, 2012 Hello. I have one programming problem. I have this log, from witch i have to read specific area of text: webtopay.log OK 123.456.7.89 [2012-03-15 09:09:59 -0400] v1.5: MIKRO to:"1398", from:"865458961", id:"13525948", sms:"MCLADM thing" So i need the script to extract word "thing" from that log. Also that script has to check if there is new entries in the log, and extract text from the last one. (Explaining in other words, that script should extract word AFTER MCLADM. Every time its a different word) p.s. I need that script to be integrated here (this has to send command to server "/manuadd (text from log)" : <?php try{ $HOST = "178.16.35.196"; //the ip of the bukkit server $password = "MCLietuva"; //Can't touch this: $sock = socket_create(AF_INET, SOCK_STREAM, 0) or die("error: could not create socket\n"); $succ = socket_connect($sock, $HOST, 4445) or die("error: could not connect to host\n"); //Authentification socket_write($sock, $command = md5($password)."<Password>", strlen($command) + 1) or die("error: failed to write to socket\n"); //Begin custom code here. socket_write($sock, $command = "/Command/ExecuteConsoleCommandAndReturn-SimpleBroadCast:broadcast lol;", strlen($command) + 1) //Writing text/command we want to send to the server or die("error: failed to write to socket\n"); sleep(2); // This is example code and here has to be that script i want to make. //while(($returnedString = socket_read($sock,50000))!= ""){ $returnedString = socket_read($sock,50000,PHP_NORMAL_READ); print($returnedString) //} print("End of script"); socket_close($sock); }catch(Exception $e){ echo $e->getMessage(); } ?> I hope i made things clear and you will help me Thanks Quote Link to comment https://forums.phpfreaks.com/topic/258987-need-help-on-writing-a-script-extract-text-from-file/ Share on other sites More sharing options...
Psycho Posted March 15, 2012 Share Posted March 15, 2012 I am willing to "help". This forum is for people wanting "help" with code they have written. If YOU have written the code above and someone provides a solution, YOU should be able to integrate it into that code. In any event, you need to be VERY explicit in these types of request. Your first request looks very simple, but you don't state whether there are multiple lines that would contain "MCLADM thing" or only one. If there are multiple, what should be the behavior? I'm also not following these two comments: Also that script has to check if there is new entries in the log, and extract text from the last one. OK, so you only want the word for the LAST entry and only if that entry is "new"? What criteria determines if an entry is new? Explaining in other words, that script should extract word AFTER MCLADM. Every time its a different word The last statement said to only retrieve the last entry (if it is new), but this one states to get the value "every time it is different". Every time it is different from what? The previous entry in the log, from the last time the log was processed, or what? Quote Link to comment https://forums.phpfreaks.com/topic/258987-need-help-on-writing-a-script-extract-text-from-file/#findComment-1327671 Share on other sites More sharing options...
sedvis Posted March 15, 2012 Author Share Posted March 15, 2012 OK, I will explain the whole thing. I have minecraft server and I use WebToPay service. When someone sends sms he types in "MCLADM (and his name here in my minecraft server). Then that message is send to my website, and is logged there in webtopay.log . The message gets logged like this: OK 178.16.35.196 [2012-03-15 09:09:59 -0400] v1.5: MIKRO to:"1398", from:"865473201", id:"13525948", sms:"MCLADM sedvis" when someone else sends message the log will look like: OK 178.16.35.196 [2012-03-15 09:09:59 -0400] v1.5: MIKRO to:"1398", from:"865473201", id:"13525948", sms:"MCLADM sedvis" OK 178.16.35.196 [2012-03-16 10:23:43 -0400] v1.5: MIKRO to:"1398", from:"865445896", id:"13526983", sms:"MCLADM petras" and so on. So this script has to check if any new messages came and if new sms came, it has to send "/manuadd (username of the sms sender (petras))" to my minecraft server, and somehow it has to be integrated with this...: <?php try{ $HOST = "178.16.35.196"; //the ip of the bukkit server $password = "MCLietuva"; //Can't touch this: $sock = socket_create(AF_INET, SOCK_STREAM, 0) or die("error: could not create socket\n"); $succ = socket_connect($sock, $HOST, 4445) or die("error: could not connect to host\n"); //Authentification socket_write($sock, $command = md5($password)."<Password>", strlen($command) + 1) or die("error: failed to write to socket\n"); //Begin custom code here. socket_write($sock, $command = "/Command/ExecuteConsoleCommandAndReturn-SimpleBroadCast:broadcast lol;", strlen($command) + 1) //Writing text/command we want to send to the server or die("error: failed to write to socket\n"); sleep(2); // This is example code and here has to be that script i want to make. //while(($returnedString = socket_read($sock,50000))!= ""){ $returnedString = socket_read($sock,50000,PHP_NORMAL_READ); print($returnedString) //} print("End of script"); socket_close($sock); }catch(Exception $e){ echo $e->getMessage(); } ?> and sorry for my english, its not my native language Quote Link to comment https://forums.phpfreaks.com/topic/258987-need-help-on-writing-a-script-extract-text-from-file/#findComment-1327680 Share on other sites More sharing options...
Psycho Posted March 15, 2012 Share Posted March 15, 2012 OK, so let me rephrase what I understand. You want all entries in the log with the "MCLADM XXXX" to be processed. But, you only want the ones that were not processed previously. OK, but how is the script supposed to determine WHICH entries are new? You might be able to use the timestamp in the log, but I assume that timestamp is determined by the Minecraft server and not the Web server. If they are not exactly synced you could end up either missing some entries or duplicating others. I would also think that emptying the log is not an option. If the PHP script was trying to save the emptied copy while the game server is trying to make a new entry there would be problems. I think the best option in this situation would be to process the log file and then store the timestamp of the last record processed. Also, are the record in chronological or reverse chronological order? I.e. are the new records at the top or the bottom. I would assume they are at the bottom and that creates another problem. As the log file grows, the time to process the log file will increase. Lastly, if there are two entries in the log file for the same user (both new since the last time the file was processed) do you want them both processed or only one. Plus, what if a log entry for a user was processed in a previous script run and a new one was added? If you don't want to process new log entries for users that were processed previously, you will need to store a record of the ones you have previously processed. Quote Link to comment https://forums.phpfreaks.com/topic/258987-need-help-on-writing-a-script-extract-text-from-file/#findComment-1327684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.