Project Evolution Posted August 1, 2010 Share Posted August 1, 2010 Hello, lately I have been having a problem with my simple PHP script. Out of the blue the following script doesnt seem to work properly, function sanitize_tags($content, $bad_tags = '') { foreach ($bad_tags as $tag) { $stripped_tags = str_replace($tag, '', $content); return $stripped_tags; } return $content; } $contents = "<html> <head> </head> <body>yutu<style>ghghl;kl;<script>hfjhk </body> </html>"; $bad_tag = array("<style>", "<script>"); $sanitize = sanitize_tags($contents, $bad_tag); echo $sanitize; Basically, when the function is invoked, only the first element in the array is used by the function. It seems like the second+ elements arent even iterated. Why is it only the first element is iterated? I have a feeling its going to be something extremely obvious. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/209480-php-for-loops-and-arrays/ Share on other sites More sharing options...
jcbones Posted August 1, 2010 Share Posted August 1, 2010 $allow_tags = '<html><body><b><a><p>'; $sanitize = strip_tags($contents, $allow_tags); echo $sanitize; Quote Link to comment https://forums.phpfreaks.com/topic/209480-php-for-loops-and-arrays/#findComment-1093761 Share on other sites More sharing options...
Project Evolution Posted August 1, 2010 Author Share Posted August 1, 2010 I understand the use of that function, however im trying to make my own implementation of it. (what I wrote wasnt the complete function) Quote Link to comment https://forums.phpfreaks.com/topic/209480-php-for-loops-and-arrays/#findComment-1093764 Share on other sites More sharing options...
dezkit Posted August 1, 2010 Share Posted August 1, 2010 Shouldn't you return $stripped_tags instead of $content at the end of the function? Quote Link to comment https://forums.phpfreaks.com/topic/209480-php-for-loops-and-arrays/#findComment-1093766 Share on other sites More sharing options...
Project Evolution Posted August 1, 2010 Author Share Posted August 1, 2010 Whoops, been messing around with the script too much. Any solution to the problem at hand? Quote Link to comment https://forums.phpfreaks.com/topic/209480-php-for-loops-and-arrays/#findComment-1093768 Share on other sites More sharing options...
dezkit Posted August 1, 2010 Share Posted August 1, 2010 <?php function sanitize_tags($content, $bad_tags = NULL) { foreach ($bad_tags as $tag) { $content = str_replace($tag, '', $content); } return $content; } $contents = "<html> <head> </head> <body>yutu<style>ghghl;kl;<script>hfjhk </body> </html>"; $bad_tag = array("<style>", "<script>"); $sanitize = sanitize_tags($contents, $bad_tag); echo $sanitize; ?> worked for me, Quote Link to comment https://forums.phpfreaks.com/topic/209480-php-for-loops-and-arrays/#findComment-1093769 Share on other sites More sharing options...
Project Evolution Posted August 1, 2010 Author Share Posted August 1, 2010 As with I, hm. Thanks for figuring out the solution, cheers. Quote Link to comment https://forums.phpfreaks.com/topic/209480-php-for-loops-and-arrays/#findComment-1093770 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.