3

I have file php:

<?php
$content = "
<script>
include string show_ads_1 and other string 1
</script>

<script>
include string show_ads_2 and other string 2
</script>

<script>
include string show_ads_x and other string x
</script>

<script>
not include 'show  _  ads  _  x' --> keeping this
</script>

<script type=\"text/javascript\">
include string 'show_ads_x' but keeping this because <script type='text/javascript'> not <script>
</script>
";

//Only remove tag <script></script> if includes "show_ads" string
$content = preg_replace('/(<script>)(show_ads.*?)(<\/script>)/s', '$2', $content);
echo $content;
?>

If content between <script>...</script> include string "show_ads_" it will remove <script> and </script> but keep all content.

But above script is not working. Nothing removed. I want when run it and view source code it looks like:

include string show_ads_1 and other string 1



include string show_ads_2 and other string 2



include string show_ads_x and other string x


<script>
not include 'show  _   ads  _  x' --> keeping this
</script>

<script type="text/javascript">
include string 'show_ads_x' but keeping this because <script type='text/javascript'> not <script>
</script>
Miss Phuong
  • 289
  • 1
  • 4
  • 15
  • 1
    And how does this "not work"? Your view source sample shows that the script tags HAVE been removed, as you state in your requirements. – Marc B Dec 06 '12 at 15:14
  • @MarcB I think what hes trying to get at; when you view over the source code the `` tags. – Daryl Gill Dec 06 '12 at 15:25
  • Possible Duplication of own question maybe? http://stackoverflow.com/questions/13738376/remove-tag-but-keep-string-between-tag-in-php – Daryl Gill Dec 06 '12 at 15:35
  • @Marc B: Run file and see blank page. View source code, nothing changed. I want page shows: "include string show_ads_1 and other string 1" "include string show_ads_2 and other string 2" "include string show_ads_n and other string n" View source code not include in below – Miss Phuong Dec 06 '12 at 15:42
  • @Daryl Gill: No sir, that question is solved but remove all . I want only remove if include string. I will delete old question – Miss Phuong Dec 06 '12 at 15:45

2 Answers2

2

Replace:

$content = preg_replace('/(<script>)(show_ads.*?)(<\/script>)/s', '$2', $content);

With:

$content = preg_replace('/(<script>)([^<]*show_ads_[^<]*)(<\/script>)/s', '$2', $content);
Felix Glas
  • 15,065
  • 7
  • 53
  • 82
0

Use DOMDocument to perform that, like this:

<?php

$dom = new DOMDocument();
$content = "
    <script>
    include string show_ads_1 and other string 1
    </script>

    <script>
    include string show_ads_2 and other string 2
    </script>
    ....
    <script>
    include string show_ads_x and other string x
    </script>

    <script>
    include string bla bla bla
    </script>

    <script>
    not include 'show  _   ads  _  x' --> keeping this
    </script>

    <script type='text/javascript'>
    must keeping script
    </script>

    <script type='text/javascript'>
    include string 'show_ads_x' but keeping this because <script type='text/javascript'> not <script>
    </script>    
";

$dom->loadHTML($content);
$scripts = $dom->getElementsByTagName('script');

foreach ($scripts as $script) {
    if (!$script->hasAttributes()) {
        if (strstr($script->nodeValue, "show_ads_")) {
            echo $script->nodeValue . "<br>";
        }
    } else {
        echo "<script type='text/javascript'>$script->nodeValue</script>" . "<br>";
    }
}

?>
Amr
  • 4,809
  • 6
  • 46
  • 60