Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php -> delete items from array which contain words from a blacklist

Tags:

arrays

php

I have got an array with several twitter tweets and want to delete all tweets in this array which contain one of the following words blacklist|blackwords|somemore

who could help me with this case?

like image 271
Lupo Avatar asked Apr 10 '26 02:04

Lupo


1 Answers

Here's a suggestion:

<?php
$banned_words = 'blacklist|blackwords|somemore';
$tweets = array( 'A normal tweet', 'This tweet uses blackwords' );
$blacklist = explode( '|', $banned_words );

//  Check each tweet
foreach ( $tweets as $key => $text )
{
    //  Search the tweet for each banned word
    foreach ( $blacklist as $badword )
    {
        if ( stristr( $text, $badword ) )
        {
            //  Remove the offending tweet from the array
            unset( $tweets[$key] );
        }
    }
}
?>
like image 176
Kalessin Avatar answered Apr 12 '26 15:04

Kalessin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!