日期:2014-05-16  浏览次数:20451 次

PHP中如何转义,大拿请帮忙!!!急急急!!!
以下new_content是mysql中使用select返回的内容

$new_content='<P>下面我给大家讲讲javascript函数:</P><PRE class=js name="code"><script> <BR>function show() <BR>{ <BR>alert("hello world"); <BR>} <BR></script></PRE> <P> 结果如图:<BR><IMG title=tu1 alt=tu1 src="uploaded/010P0000240111S5344-1.jpg" width=150 height=150 data-mce-src="uploaded/010P0000240111S5344-1.jpg"></P>';

请问如何将其中的<script>和</script>转换成&lt;script&gt;和&lt;/script&gt;
------解决方案--------------------
htmlspecialchars这个函数可以转义
------解决方案--------------------
$new_content='<P>下面我给大家讲讲javascript函数:</P><PRE class=js name="code"><script> <BR>function show() <BR>{ <BR>alert("hello world"); <BR>} <BR></script></PRE> <P> 结果如图:<BR><IMG title=tu1 alt=tu1 src="uploaded/010P0000240111S5344-1.jpg" width=150 height=150 data-mce-src="uploaded/010P0000240111S5344-1.jpg"></P>';
var_dump(preg_replace_callback('/<\/?script>/i', function($r){return htmlentities($r[0]);}, $new_content)) ;