Warning: Unexpected character in input: '\' (ASCII=92) state=1

I receive the following error message:

Warning: Unexpected character in input: '' (ASCII=92) state=1

And this is the line of code that is giving me this trouble.

$tag_value = preg_replace('/{(.*?)}/e', '$values[1]', $tag_value);

I am using PHP 5.2.9 and upgrading is not an option.

Regular expression are not my specialty and I am not able to solve this problem on my own. Any help would be greatly appreciated.


You cannot accomplish this with a simple preg_replace , as array de-referencing is not done with /e modifier. Instead you can use preg_replace_callback function:

$tag_value = preg_replace_callback("/{(.*?)}/", function($m) use($values){
    return $values[$m[1]];
}, $tag_value);

This definitely works in php 5.3, however in 5.2 you may need to define the callback function explicitly:

function replace($m) {
    global $values;
    return $values[$m[1]];
}
$tag_value = preg_replace_callback("/{(.*?)}/", "replace", $tag_value);

EDIT: The error you are seeing is happening because with your original code, your substitution is being treated literally as $values[1] (after unescaping the backslash - in this string, 1 is not the right stuff to put inside the brackets.

链接地址: http://www.djcxy.com/p/69480.html

上一篇: 警告:输入中出现意外的字符:'\'(ASCII = 92)状态= 0

下一篇: 警告:输入中出现意外字符:'\'(ASCII = 92)状态= 1