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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.