Jump to content

preg_match


Baving

Recommended Posts

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 line
if($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]
Link to comment
https://forums.phpfreaks.com/topic/33796-preg_match/#findComment-158526
Share on other sites

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 line
if($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]
Link to comment
https://forums.phpfreaks.com/topic/33796-preg_match/#findComment-158540
Share on other sites

Try this:
[code]
<?php
function 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.
Link to comment
https://forums.phpfreaks.com/topic/33796-preg_match/#findComment-158726
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.