1

I have written a module for a PHP IRC bot which uses a database for saving which nicks are "linked" to a Wikipedia account. To do this, I have the following code:

$pudb_wnas = file('cache.ucb');
foreach ($pudb_wnas as $lineNumber => $line) {
    if (preg_match("/" . p_chars($ex[4]) . "~!/i", $line) != false) {
        $found = true;
        break;
    }
}
if ($found) {
    echo "!!!!!!!!!!!!!!!!FOUND!!!!!!!!!!!!!!!!!!\n";
    $lines = file('cache.ucb', FILE_IGNORE_NEW_LINES);
    $wnas_bu = $lines[$lineNumber];                                                                                                                                            
    $lines[$lineNumber] = trim($wnas_bu) . "~!" . $mask[0];                                                                                                                    
    file_put_contents('cache.ucb', implode("\n", $lines));                                                                                                                     
} else {
    $udb_wnas = fopen("cache.ucb", "a+");
    fwrite($udb_wnas, "\n" . p_chars($ex[4]) . "~!" . $mask[0]);
    fclose($udb_wnas);
}

The p_chars function, alongside with dp_chars, is a custom function to add or remove backslashes to some special characters:

function p_chars( $text ) {
    $m_text = str_replace("(", "\(", $text);
    $m_text = str_replace(")", "\)", $m_text);
    return $m_text;
}

// Función para descodificar p_chars
function dp_chars( $text ) {
    $m_text = str_replace("\(", "(", $text);
    $m_text = str_replace("\)", ")", $m_text);
    return $m_text;
}

The cache.ucb file (which is really nas.udb, but that's another story) contents are located here. You can also see the full code here.

The variable $ex[4], in this context, means the argument passed to the %nas command, which is used from IRC like this: %nas Test.

When you call the command, it searches on the cache.ucb file for a line starting with $ex[4]."~!". I use ~! as a separator. For example, if the command arguments were %nas Testing, it'd search for Testing~!. If it founds the line, it replaces it with itself plus "~!".$mask[0]. $mask[0] is the nickname of the user who called the command.

If it doesn't find the line, it'll add a new one following this structure: $ex[4]~!$mask[0]. So, as an example, if Bousie did %nas Testing, it would search for Testing~!. Assuming the line was Testing~!Test, it would replace it with Testing~!Test~!Bousie. Else, it'd add the line Testing~!Bousie.

The problem is that, if Bousie did %nas UnRar, it'd work properly. But if Bousie did %nas I(L)Verano, it'd add a new line. But there's already the I(L)Verano line. Note that it should convert the I(L)Verano argument to I\(L\)Verano before sarching it, so I don't know what I'm doing wrong on this code.

You'd really help me telling me what I'm doing wrong. Oh, and I tried with lots of codes I saw here, I tried mixing them and... Perhaps the problem is on the p_chars and dp_chars functions? I can't see it, though!

4

1 回答 1

1

所有你需要的是

$file = "cache.ucb";
$ex[4] = "I\(L\)Verano";
$nick = "Bousie";
$pudb_wnas = file($file, FILE_IGNORE_NEW_LINES);

var_dump($pudb_wnas); // before
foreach ( $pudb_wnas as $k => &$line ) {
    preg_match(sprintf("/^%s~/", preg_quote($ex[4])), $line) and $line = sprintf("%s~!%s", $line, $nick);
}
var_dump($pudb_wnas); // after
file_put_contents($file, implode(PHP_EOL, $pudb_wnas));

array (size=4)
  0 => string 'UnRar~!Catbuntu' (length=15)
  1 => string 'MistrX~!MRX' (length=11)
  2 => string 'I\(L\)Verano~!ILVerano' (length=22)
  3 => &string '-jem-~!jem-' (length=11)

array (size=4)
  0 => string 'UnRar~!Catbuntu' (length=15)
  1 => string 'MistrX~!MRX' (length=11)
  2 => string 'I\(L\)Verano~!ILVerano~!Bousie' (length=30)  // works perfectly 
  3 => &string '-jem-~!jem-' (length=11)
于 2012-12-19T20:30:44.197 回答