Home >Backend Development >PHP Tutorial >php 正则替换问题

php 正则替换问题

WBOY
WBOYOriginal
2016-06-23 13:48:54958browse

替换 script  代码部分注释//....(//开始到行尾) 为空

$str="<script> document.write(unescape(\"%3Cspan id='szgs_stat_icon_2'%3E%3C/span%3E%3Cscript src='http://www.szgswljg.gov.cn/sendMark?siteid=2&website=z.com' type='text/javascript'%3E%3C/script%3E\")); </script><script type=\"text/javascript\" src=\"http://z.com/a.js\"></script><!--[if IE 6]><script type=\"text/javascript\" src=\"http://z.com/pngfix.js\"></script><![endif]--><script src=\"http://z.com/jquery.uploadify.3.2.1.js\"></script><script>//true56546456function showTips(type,msg,d){//true存在文件,false不存在文件$('#'+type).parent().find('span').text(msg).removeClass('Validform_right').addClass('Validform_wrong');}//truedddddddddvar uploadFile=false,//true存在文件,false不存在文件dia,upToken='roS2qYSKuPqZB7rZmvfpZy';//true存在文件,false不存在文件</script><span style=\"display:None;\"><script type=\"text/javascript\">var _bdhmProtocol = ((\"https:\" == document.location.protocol) ? \" https://\" : \" http://\");document.write(unescape(\"%3Cscript src='\" + _bdhmProtocol + \"hm.baidu.com/h.js?28' type='text/javascript'%3E%3C/script%3E\"));</script></span>2222222222222";

我弄了一个,不正确
$str=preg_replace('/(<script>[\s\S]*?)[^(http:|https:)]\/\/.*?\n([\S\s]*?<\/script>)/si','$1$2',$str);echo $str;


回复讨论(解决方案)

$s =<<< TXT<script> document.write(unescape(\"%3Cspan id='szgs_stat_icon_2'%3E%3C/span%3E%3Cscript src='http://www.szgswljg.gov.cn/sendMark?siteid=2&website=z.com' type='text/javascript'%3E%3C/script%3E\")); </script><script type=\"text/javascript\" src=\"http://z.com/a.js\"></script><!--[if IE 6]><script type=\"text/javascript\" src=\"http://z.com/pngfix.js\"></script><![endif]--><script src=\"http://z.com/jquery.uploadify.3.2.1.js\"></script><script>//true56546456function showTips(type,msg,d){//true存在文件,false不存在文件$('#'+type).parent().find('span').text(msg).removeClass('Validform_right').addClass('Validform_wrong');}//truedddddddddvar uploadFile=false,//true存在文件,false不存在文件dia,upToken='roS2qYSKuPqZB7rZmvfpZy';//true存在文件,false不存在文件</script><span style=\"display:None;\"><script type=\"text/javascript\">var _bdhmProtocol = ((\"https:\" == document.location.protocol) ? \" https://\" : \" http://\");document.write(unescape(\"%3Cscript src='\" + _bdhmProtocol + \"hm.baidu.com/h.js?28' type='text/javascript'%3E%3C/script%3E\"));</script></span>2222222222222TXT;echo preg_replace('#([^:])//.+#m', '$1', $s);
<script> document.write(unescape(\"%3Cspan id='szgs_stat_icon_2'%3E%3C/span%3E%3Cscript src='http://www.szgswljg.gov.cn/sendMark?siteid=2&website=z.com' type='text/javascript'%3E%3C/script%3E\")); </script><script type=\"text/javascript\" src=\"http://z.com/a.js\"></script><!--[if IE 6]><script type=\"text/javascript\" src=\"http://z.com/pngfix.js\"></script><![endif]--><script src=\"http://z.com/jquery.uploadify.3.2.1.js\"></script><script>function showTips(type,msg,d){$('#'+type).parent().find('span').text(msg).removeClass('Validform_right').addClass('Validform_wrong');}var uploadFile=false,dia,upToken='roS2qYSKuPqZB7rZmvfpZy';</script><span style=\"display:None;\"><script type=\"text/javascript\">var _bdhmProtocol = ((\"https:\" == document.location.protocol) ? \" https://\" : \" http://\");document.write(unescape(\"%3Cscript src='\" + _bdhmProtocol + \"hm.baidu.com/h.js?28' type='text/javascript'%3E%3C/script%3E\"));</script></span>2222222222222

GOOD 精辟

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn