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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.