I am trying to replace every character (including newline, tabs, whitespace etc)
between Nodes that has the same tag name. The problem is that the regex matches the different node (string) as one based on similarity between the beginning and closing tags of the nodes and then output a single result.
For Example:
$html_string = "
<div> Below are object Node with the html code </div>
<script> alert('i want this to be replaced. it has no newline'); </script>
<div> I don't want this to be replaced </div>
<script>
console.log('i also want this to be replaced. It has newline');
</script>
<div> This is a div tag and not a script, so it should not be replaced </div>
<script> console.warn(Finally, this should be replaced, it also has newline');
</script>
<div> The above is the final result of the replacements </div> ";
$regex = '/(?:\<script\>)(.*)?(?:\<\/script\>)/ims';
$result = preg_replace($regex, '<!-- THIS SCRIPT CONTENT HERE HAS BEEN ALTERED -->', $html_string);
echo $result;
Expected Result:
<div> Below are object Node with the html code </div>
<!-- THIS SCRIPT CONTENT HERE HAS BEEN ALTERED -->
<div> I don't want this to be replaced </div>
<!-- THIS SCRIPT CONTENT HERE HAS BEEN ALTERED -->
<div> This is a div tag and not a script, so it should not be replaced </div>
<!-- THIS SCRIPT CONTENT HERE HAS BEEN ALTERED -->
<div> The above is the final result of the replacements </div>
Actual Output:
<div> Below are object Node with the html code </div>
<!-- THIS SCRIPT CONTENT HERE HAS BEEN ALTERED -->
<div> The above is the final result of the replacements </div>
How can i sort this out. Thanks in advance.