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. Link to comment https://forums.phpfreaks.com/topic/55678-replace-tags/ 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) Link to comment https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275161 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. Link to comment https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275516 Share on other sites More sharing options...
Lumio Posted June 15, 2007 Share Posted June 15, 2007 Then use strip_tags() Link to comment https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275520 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. Link to comment https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275524 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>~ Link to comment https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275528 Share on other sites More sharing options...
The Little Guy Posted June 15, 2007 Author Share Posted June 15, 2007 That doesn't work Link to comment https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275530 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 Link to comment https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275542 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; } ?> Link to comment https://forums.phpfreaks.com/topic/55678-replace-tags/#findComment-275554 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.