Search code examples
phpregexpreg-replacemirc

Undefined index notice in php regexp


Well, I've tried searching over the internet for a few hours now, but so far it's been of no assistance. I am trying to figure out why I keep getting the following notice notice(yes, I am aware it is just a notice and does not halt the execution of the script):

Notice: Undefined index: 05 in [redacted_path]\script.php(31) : regexp code on line 1

This script was meant as a color re-placer for irc channel logs; I've grabbed the large function from another site and modified it slightly to suit my needs. Credit of course goes to its creator. That aside, I am.. lightly familiar with regex, but not the point where I can of course spot the offending typo or similar below. Any assistance would be appreciated.

<?php
if (isset($_POST['chn'])) {
    $chn = $_POST['chn'];
    $str = urldecode($_POST['line']);
    $filen = "logs/$chn" . "_" . date('Y_F_d') . ".html";
    $str = preg_replace('/https?:\/\/[^\s"<>]+/', '<a href="$0" target="_blank">$0</a>', $str);
    $logln = mirc2html($str) . "<br />\r\n";
    $logln = str_replace("Â","",$logln);
    file_put_contents($filen,stripcslashes($logln),FILE_APPEND);
}

if (isset($_GET['test'])) {
    echo str_replace("Â","",mirc2html($_GET['test']));
}

function mirc2html($x) {
    $c = array("FFF","000","00007F","009000","FF0000","7F0000","9F009F","FF7F00","FFFF00","00F800","00908F","00FFFF","0000FF","FF00FF","7F7F7F","CFD0CF");
    $x = preg_replace("/\x02(.*?)((?=\x02)\x02|$)/", "<b>$1</b>", $x);
    $x = preg_replace("/\x1F(.*?)((?=\x1F)\x1F|$)/", "<u>$1</u>", $x);
    $x = preg_replace("/\x1D(.*?)((?=\x1D)\x1D|$)/", "<i>$1</i>", $x);
    $x = preg_replace("/\x03(\d\d?),(\d\d?)(.*?)(?(?=\x03)|$)/e", "'<span style=\"color: #'.\$c['$1'].'; background-color: #'.\$c['$2'].';\">$3</span>'", $x, -1, $n1);
    $x = preg_replace("/\x03(\d\d?)(.*?)(?(?=\x03)|$)/e", "'</span><span style=\"color: #'.\$c['$1'].';\">$2</span>'", $x);
    $x = preg_replace("/\x03|\x0F(.*?)/", "<span style=\"color: #000; background-color: #FFF;\">$1</span>", $x);
    $x = str_replace("\n","",$x);
    $x = str_replace("\r","",$x);
    $x = preg_replace("/\<\/span\>/","",$x,1);
    $x = preg_replace("/(\<\/span\>){2}/","</span>",$x);
    return $x;
}
?>

The offending line is thus:

$x = preg_replace("/\x03(\d\d?)(.*?)(?(?=\x03)|$)/e", "'</span><span style=\"color: #'.\$c['$1'].';\">$2</span>'", $x);

Solution

  • The cause of your error is clear: The variable $c['05'] is not defined. Define it or change the replacement with a function that is able to convert it into an integer that - for some reason I'm not clear about - PHP is not able with that eval'ed regex replacement (normally numerical strings are converted into integer for keys automatically internally).

    If you're open for some suggestions:

    • Refactor the code to remove the duplicate parts (It's always some code that does something, multiple classes of codes, each class has it's own replacements).
    • Do the replacement with a callback function so that you can better control the replacement and pass the color values.