What are the advantages and disadvantages of using a text filter versus a captcha for preventing spam posts on a PHP-based website?

One common issue on PHP-based websites is preventing spam posts. Two common methods to address this issue are using a text filter to detect and block spam content or implementing a captcha to verify that the user is human. Using a text filter can be advantageous as it can automatically detect and block spam posts based on certain keywords or patterns. However, it may not be as effective against more sophisticated spam attacks. On the other hand, captchas can provide a more secure way to verify user identity, but they can also be intrusive and deter legitimate users.

// Example of implementing a text filter to prevent spam posts
function filter_spam_content($content){
    $spam_keywords = array("viagra", "online casino", "free money");
    
    foreach($spam_keywords as $keyword){
        if(stripos($content, $keyword) !== false){
            // Block post if spam keyword is found
            return false;
        }
    }
    
    return true;
}

// Example usage
$post_content = $_POST['content'];
if(filter_spam_content($post_content)){
    // Process the post
} else {
    // Display an error message
    echo "Your post contains spam content.";
}