preg_replace错误不知道为什么

时间:2012-11-02 03:02:07

标签: html dom replace

有这个代码......它假设从表单中删除一个非常具体的JS行来生成一个无效的html,这样我以后可以使用dom解析器,但它会给出这个错误

Warning: preg_replace(): Compilation failed: missing ) at offset 29 in E:\xampp-portable\htdocs\ingeniat\system\replace_html.php on line 18

这是代码我不知道为什么会发生这种情况希望你们能帮忙!

<?php
//<input class="correct" readonly="readonly" onmouseover="return overlib('<div 
//class="correctness">Respuesta correcta: -2</div>', STICKY, MOUSEOFF, CAPTION, '
//Respuesta correcta', FGCOLOR, '#FFFFFF');" onmouseout="return nd();" 
//name="resp636911_2" value="-2" size="2" type="text">


$cont = file_get_contents('../test_files/answer.php');
$patterns = array();
$patterns[0] = '/onmouseover="return overlib('."'/";
$patterns[1] = "/', STICKY, MOUSEOFF, CAPTION, 'Respuesta correcta', FGCOLOR, '#FFFFFF');".'" onmouseout="return nd();"/';
$replacements = array();
$replacements[0] = '>';
$replacements[1] = '';
echo preg_replace($patterns, $replacements, $cont);

?>

我在目标代码开头的注释中定位HTML的片段。

1 个答案:

答案 0 :(得分:0)

首先,你需要在正则表达式模式上转义所有特殊字符。您可以在此处查看详细信息:http://www.regular-expressions.info/characters.html

试试这段代码:

$cont = '<input class="correct" readonly="readonly" onmouseover="return overlib(\'<div 
class="correctness">Respuesta correcta: -2</div>\', STICKY, MOUSEOFF, CAPTION, \'Respuesta correcta\', FGCOLOR, \'#FFFFFF\');" onmouseout="return nd();" 
name="resp636911_2" value="-2" size="2" type="text">';

$patterns = array();
$patterns[0] = "/onmouseover=\"return overlib\(\'/s";
$patterns[1] = "/\'\, STICKY\, MOUSEOFF\, CAPTION\, \'Respuesta correcta\', FGCOLOR\, \'#FFFFFF\'\);\"\sonmouseout=\"return nd\(\);\"/";

$replacements = array();
$replacements[0] = '>';
$replacements[1] = '';

$var = preg_replace($patterns, $replacements, $cont);
var_dump($var);