この複数行を str_replace で削除したい
<div><i>Hello</i><br/>
<!-- Begin: Hello.text -->
<script src="http://site.com/hello.php?id=12345" type="text/javascript">
</script>
<!-- End: Hello.text --></div>
12345 = 乱数
この複数行を str_replace で削除したい
<div><i>Hello</i><br/>
<!-- Begin: Hello.text -->
<script src="http://site.com/hello.php?id=12345" type="text/javascript">
</script>
<!-- End: Hello.text --></div>
12345 = 乱数
これを試して:
$pattern = <<< EOD
@<div><i>Hello</i><br/>
<!-- Begin: Hello\.text -->
<script src="http://site\.com/hello\.php\?id=\d++" type="text/javascript">
</script>
<!-- End: Hello\.text --></div>
?@
EOD;
echo preg_replace($pattern,'',$text);
<?php
$html = '<div><i>Hello</i><br/><!-- Begin: Hello.text --><script src="http://site.com/hello.php?id=12345" type="text/javascript"></script><!-- End: Hello.text --></div>';
$doc = new DOMDocument();
// load the HTML string we want to strip
$doc->loadHTML($html);
// get all the script tags
$script_tags = $doc->getElementsByTagName('script');
$length = $script_tags->length;
// for each tag, remove it from the DOM
for ($i = 0; $i < $length; $i++) {
$script_tags->item($i)->parentNode->removeChild($script_tags->item($i));
}
// get the HTML string back
$no_script = $doc->saveHTML();
echo $no_script;
// this removes every script tag from your input ($html)
ここから撮影
実際の例はこちら