1

I have been searching on Google and this platform to find an answer but I am unable to make it work. So here is my situation:

What I want:

To check for each object if it contains a string which is defined in an array and only output objects which pass this condition.

What I got:

$mail_body = "<table>.....</table>";
$blacklist = array("PCabc", "PCxyz");

foreach($blacklist as $blacklists){
              if (strpos($mail_body, $blacklists) !== false){


                echo "<br>".$mail_body."<br>";
              }
}

Turns out. This code is actually a working whitelist.^^

But I want the opposite but it would only output an object for each string within the array if I change !== false to == false. So if I got 5 items in $blacklist it would output 5 times the same for every object.

0

2 Answers 2

2

You need to put echo out of the loop. For example:

$mail_body = "<table>.....</table>";
$blacklist = array("PCabc", "PCxyz");
$output = true;

foreach($blacklist as $blacklists){
    if (strpos($mail_body, $blacklists) !== false){
        $output = false;
        break;
    }
}

if ($output) {
    echo "<br>".$mail_body."<br>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

!== false and add a break. No need to continue with iteration.
2

A little workaround, come out of foreach LOOP, and set a flag for existence of balacklisted words.After that based on the value of isBlackListed, proceed

$mail_body = "<table>.....</table>";
$blacklist = array("PCabc", "PCxyz");

$isBlackListed = false;  //initialize to false

foreach($blacklist as $blacklists){
         if (strpos($mail_body, $blacklists) !== false){
              $isBlackListed = true;  
              //on encounter of blacklisted word , set this to true
        }
}

// proceed based on $isBlacklisted value

if( !$isBlackListed)
    echo "<br>".$mail_body."<br>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.