>백엔드 개발 >PHP 튜토리얼 >php 正则替换问题

php 正则替换问题

WBOY
WBOY원래의
2016-06-23 13:48:54961검색

替换 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 精辟

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.