-1

有这个代码......它假设从表单中删除一个非常具体的 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 片段。

4

1 回答 1

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);
于 2012-11-02T03:27:06.870 回答