1. When I use preg_replace with the e modifier, the replaced text seems to get extraneous slashes added to it.

For example, when I add emoticons like this:

$data = preg_replace('/ ([:8=][^\\s]+?) /e', '(($emoticons[\'$1\']) ? (\'<!--I$1--><img src="\' . $emoticons[\'$1\'] . \'" class="emot" />\') : \'$1\')', $data);

something like ="" (which is in HTML tags, it's not an emoticon) gets converted to =\"".

This is fixed when I change the \'$1\' to "$1". Does the e modifier expect back references to be quoted in double quotes, not single? I'd like to understand what's causing this.

(Disregard question #2, I think I've found the problem.)

    Well, if you look at the syntax highlighting of what you posted, you'll see some confusion about which bits are strings and which aren't. Due to a confusion about which quotes are being used to quote what.

      It looks like vBulletin destroyed what I posted - all the backslashes were taken out. :/ What I posted originally was a correctly quoted string.

      Basically, when I use e, it seems that PHP is adding slashes incorrectly when I try to use single quotes in the code that gets executed - the first double-quote character gets an extra backslash added to it.

      The code WORKS when I use double-quotes instead, so I was able to work around it, but I was hoping to learn why so that I can be sure the method I used isn't buggy.

        You could go back and replace the PHP pseudotags with CODE ones - I find that vBulletin doesn't mangle things then.

        E.g.,

        $data = preg_replace('/ ([:8=][^\s]+?) /e',
        "(\$emoticons['$1']) ? ('<!--I$1--><img src=\"\$emoticons['$1']\" class=\"emot\" />') : '$1'",
        $data);
        

        But I think the thing could be done with a single str_replace anyway. You've got an array of $emoticons, keyed by emoticon. You go through the string looking for emoticons you've got images for, and replace them with appropriate <img> tags. Ergo:

        $search=array_keys($emoticons);
        $replace=array();
        foreach($emoticons as $key=>$emoticon)
        { $replace[]="<!--$key--><img src=....$emoticon...>"; //etc.
        }
        
        $data = str_replace($search, $replace, $data);
        
          Write a Reply...