0

この複数行を 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 = 乱数

4

2 に答える 2

3

これを試して:

$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);
于 2013-05-13T15:25:36.450 に答える
0
<?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)

ここから撮影

実際の例はこちら

于 2013-05-13T14:56:43.270 に答える