The Little Guy Posted June 15, 2007 Share Posted June 15, 2007 I am trying to use this, to find style or script tags, then replace them and everything between them with a space: <?php $body = preg_replace("~<style|script.*>.*</style|script>~"," ",$body); ?> The code obviously doesn't work. please help. Quote Link to comment Share on other sites More sharing options...
Lumio Posted June 15, 2007 Share Posted June 15, 2007 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) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 15, 2007 Author Share Posted June 15, 2007 No... I just want to remove the tags, and everything between them. Quote Link to comment Share on other sites More sharing options...
Lumio Posted June 15, 2007 Share Posted June 15, 2007 Then use strip_tags() Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 15, 2007 Author Share Posted June 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted June 15, 2007 Share Posted June 15, 2007 You need to limit the alternation: ~<(style|script)[^>]*>.*?</\1>~ Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 15, 2007 Author Share Posted June 15, 2007 That doesn't work Quote Link to comment Share on other sites More sharing options...
effigy Posted June 15, 2007 Share Posted June 15, 2007 Add the s modifier at the end: ...~s Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 15, 2007 Author Share Posted June 15, 2007 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; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.