PHP删除相邻的关闭和打开脚本标记

Say I have

<script>alert("hello");</script><script>alert("world");</script>

How could I remove the </script><script> keeping in mind that there may or may not be a space in between (</script> <script>) and I would not want to do this if either of the script tags has the source attribute (src) set.

This should work with as many script tags as possible, so if there were three in a row that follow the same requirements, it would be turned into one big script tag.

You state that there may be "a" space. If that is the case, you have two situations: </script><script> and </script> <script>. You can easily replace both with a space:

$html = str_replace('</script><script>',' ',$html);
$html = str_replace('</script> <script>',' ',$html);

But, what if there is a lot of whitespace? Tabs, newlines, etc... all add up to whitespace. You can preg_replace (which is NOT parsing HTML with regex - it is just replacing a clearly defined string).

$html = preg_replace('~</script>\s*<script>~',' ',$html);

NOTE: If the second <script> is actually <script src='blahblahblah'>, it won't match using either str_replace or preg_replace. That is a non-issue.

use

 $pattern="/<script[^s>]*>\s*<\/script>/isU";
 $html=preg_replace($pattern,"",$html);

pattern look for script tag that have no src attribute basically we are finding if it has s inside or not and remove that script opening and closing tag seperated by zero or more space

@kainaw's answer is correct, but i have here one sophisticated version, where you can allow just some tags, and will return space, that was present between them.

function remove_same_openclosed_tags($matches){
  $allowed_tags = ['em', 'u', 'span', 'strong', 'script'];
  if(in_array($matches[1], $allowed_tags) && $matches[1] == $matches[3] ) {
    return $matches[2];
  }
  return $matches[0];
}
$content = preg_replace_callback("/<\/([a-zA-Z]+?)>(\s*)<([a-zA-Z]+?)>/is", 'remove_same_openclosed_tags' , $content);