Baving Posted January 11, 2007 Share Posted January 11, 2007 Hello,If I was to type in something like /ban username reason into a chatroom how would I get it to recognise the /ban and then split (retrieve) the username and reason so they can be processed into a SQL query.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/33796-preg_match/ Share on other sites More sharing options...
Philip Posted January 11, 2007 Share Posted January 11, 2007 This should work... somebody else will probably have a better solution.[code]<?php$line = "/ban KingPhilip For being dumb"; // -action|username|reasons$line_exploded = explode(" ", $line); //explode lineif($line_exploded[1] = "/ban") { // if action is ban $username = $line_exploded[2]; // get username unset($line_exploded[1]); // unset the username and action unset($line_exploded[2]); array_keys($line_exploded); // renumber the array foreach($line_exploded as $var) { $reason. = $var." "; // put the reason back together with spaces } mysql_query("UPDATE `user_table` SET `ban`='true' `ban_reason`='".$reason."' WHERE `username`='".$username."'") or die(mysql_error()); // run mysql query}?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/33796-preg_match/#findComment-158526 Share on other sites More sharing options...
Baving Posted January 11, 2007 Author Share Posted January 11, 2007 Following error message: Parse error: parse error on line: 11I did remove the dot on line 11 to: -[code=php:0] $reason = $var." "; [/code]Which gave: -Reason: dumbUsername: For Quote Link to comment https://forums.phpfreaks.com/topic/33796-preg_match/#findComment-158529 Share on other sites More sharing options...
Philip Posted January 11, 2007 Share Posted January 11, 2007 Well, the . is there because it continues the variable... other wise you'll just get the last word + a space.[code]<?php$line = "/ban KingPhilip For being dumb"; // -action|username|reasons$line_exploded = explode(" ", $line); //explode lineif($line_exploded[1] = "/ban") { // if action is ban $username = $line_exploded[2]; // get username unset($line_exploded[1]); // unset the username and action unset($line_exploded[2]); array_keys($line_exploded); // renumber the array $reason = ""; foreach($line_exploded as $var) { $reason. = $var; // put the reason back together with spaces $reason. = " "; } mysql_query("UPDATE `user_table` SET `ban`='true' `ban_reason`='".$reason."' WHERE `username`='".$username."'") or die(mysql_error()); // run mysql query}?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/33796-preg_match/#findComment-158540 Share on other sites More sharing options...
Philip Posted January 11, 2007 Share Posted January 11, 2007 I noticed the modify button is missing...you can delete line 8, array_keys Quote Link to comment https://forums.phpfreaks.com/topic/33796-preg_match/#findComment-158541 Share on other sites More sharing options...
Draicone Posted January 11, 2007 Share Posted January 11, 2007 Try this:[code]<?phpfunction banUser($username,$reason) { // Code to ban a user}$line = "/ban ABC For being ABC";preg_match("/\/ban ([\w]+) (.+)/i",$line,$matches);if(isset($matches)) { banUser($matches[1],$matches[2]);}?>[/code]You'll have to write the code to ban the user. It compiles perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/33796-preg_match/#findComment-158726 Share on other sites More sharing options...
Draicone Posted January 11, 2007 Share Posted January 11, 2007 Sorry, improvement on that code:[code]<?phpfunction banUser($username,$reason) { // Code to ban user}$line = "/ban ABC For being ABC";if(preg_match("/\/ban ([\w]+) (.+)/i",$line,$matches)) banUser($matches[1],$matches[2]);?>[/code]Where's the edit function? :P Quote Link to comment https://forums.phpfreaks.com/topic/33796-preg_match/#findComment-158731 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.