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

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.