繁体   English   中英

仅使用preg_match_all提取开始和结束脚本标签

[英]Extract only begining and end script tag with preg_match_all

HTML:

<script type="text/javascript"> ..code.. </script>
<script type="text/javascript"> ..code.. </script>
<script> ..code.. </script>
<script type="text/javascript"> ..code.. </script>

我想看到的是:

<script type="text/javascript"></script>
<script type="text/javascript"></script>
<script></script>
<script type="text/javascript"></script>

我的表情:

preg_match_all('/<script.*> (<\/script>)/i',$html, $result);

我不能工作。

这里

function stripscript($code) {
  $code = preg_replace('/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/i', '<script type="text/javascript"></script>', $code);
  return $code;
}

键盘示例: 此处

您可以使用此:

$html = preg_replace('~<script[^>]*>\K[^<]*(?=</script>)~i', '', $html);

或以此获得更多表演:

$html = preg_replace('~<script[^>]*+>\K[^<]*+(?=</script>)~i', '', $html);

请注意,如果确定小写字母,可以删除i

\\K从比赛结果重置比赛开始。

(?=</script>)是零宽度的断言,表示“紧随</string> ”。 这不是比赛结果的一部分。 这只是一张支票。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM