Jump to content

PHP for loops and arrays


Project Evolution

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/209480-php-for-loops-and-arrays/
Share on other sites

<?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,

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.