Jump to content

Replace tags


The Little Guy

Recommended Posts

Why do you want to replace the content?

I just eliminate the script and display it:

<?php
   $body = '<a href="#">empty link</a> <script type="text/javascript"> alert("foo=bar"); </script>';
   $body = preg_replace_callback('/(\<script.*?\>.*?\<\/script[\s]*\>)/', create_function('$a', 'return htmlspecialchars($a[0]);'), $body);
?>

If you just want to remove those script tags, you can also use strip_tags() (www.php.net/strip_tags)

Link to comment
https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275161
Share on other sites

that won't work... I am talking about tags that are between the head, something like this:

<script type="text/javascript">
   function clickMe(){
      document.write = "Im text!";
   }
</script>

 

OR

 

<style type="text/css">
   h1{
      font-size:30px;
   }
</style>

 

I would like to remove the script, and style tags and everything between them with a space.

Link to comment
https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275524
Share on other sites

I found these two functions in my book, I just use the remove() function, which uses the parse_array() function

 

<?php
function parse_array($string, $beg_tag, $close_tag)
    {
    preg_match_all("($beg_tag(.*)$close_tag)siU", $string, $matching_data);
    return $matching_data[0];
    }

function remove($string, $open_tag, $close_tag)
    {
    # Get array of things that should be removed from the input string
    $remove_array = parse_array($string, $open_tag, $close_tag);
    
    # Remove each occurrence of each array element from string;
    for($xx=0; $xx<count($remove_array); $xx++)
        $string = str_replace($remove_array, " ", $string);
    
    return $string;
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275554
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.