I am focusing on transforming a website from C# to WordPress. The website uses a widget that shows random quotes that have profanity but should be strained out. The necessity is the fact that basically the very first letter is changed by having an asterisk.

For instance "Shit" and "shit" could be changed with S*** and s*** correspondingly no matter situation. Also shithead would become s***mind permitting for partial matches which I am told is alright. What to become strained have been in a table so it's as much as the customer to define what's going to be censored.

The course in C# is:

using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class BadWordFilter
{
    private List<CRegex> patterns;
    private static BadWordFilter m_instance = null;

    public List<CRegex> Patterns
    {
        get { return patterns; }
        set { patterns = value; }
    }
    private BadWordFilter()
    {
        patterns = new List<CRegex>();
    }

    public static BadWordFilter Instance
    {
        get
        {
            if ( m_instance == null )
                m_instance = CreateBadWordFilter();

            return m_instance;
        }
    }

    protected static BadWordFilter CreateBadWordFilter()
    {
        BadWordFilter filter = new BadWordFilter();

        try
        {
            using ( DbConnection conn = provider.CreateConnection() )
            {
                conn.ConnectionString = connString;

                using ( DbCommand cmd = conn.CreateCommand() )
                {
                    string sqlQuery = "SELECT word FROM filter_words;";

                    cmd.CommandText = sqlQuery;
                    cmd.CommandType = CommandType.Text;

                    conn.Open();

                    using ( DbDataReader rdr = cmd.ExecuteReader() )
                    {
                        if ( rdr.HasRows )
                        {
                            while ( rdr.Read() )
                            {
                                //Split each word into a character array
                                char[] characters = rdr["word"].ToString().ToCharArray();

                                //We need a fast way of appending to an exisiting string
                                StringBuilder patternBuilder = new StringBuilder();

                                //The start of the patterm
                                patternBuilder.Append( "(" );

                                //We next go through each letter and append the part of the pattern.
                                //It is this stage which generates the upper and lower case variations
                                for ( int j = 0; j < characters.Length; j++ )
                                {
                                    patternBuilder.AppendFormat( "[{0}|{1}][\\W]*", characters[j].ToString().ToLower(), characters[j].ToString().ToUpper() );
                                }

                                //End the pattern
                                patternBuilder.Append( ")" );

                                //Add the new pattern to our list.
                                filter.Patterns.Add( new CRegex( rdr["word"].ToString(), patternBuilder.ToString() ) );
                            }
                        }
                    }
                }
            }
        }
        catch
        {
        }
        return filter;
    }
    public string GetCleanString( string input )
    {
        for ( int i = 0; i < patterns.Count; i++ )
        {
            //In this instance we actually replace each instance of any bad word with a specified string.
            input = patterns[i].Replace( input, String.Format( "{0}{1} ", patterns[i].Word[0], new String( '*', patterns[i].Word.Length - 1 ) ) );
        }

        //return the manipulated string
        return input;
    }

}

public class CRegex : Regex
{
    private string msWord;

    public string Word
    {
        get { return msWord; }
        set { msWord = value; }
    }

    public CRegex( string word, string pattern ) : base( pattern )
    {
        msWord = word;
    }

}

that is implimented as:

string sQuote = BadWordFilter.Instance.GetCleanString( QuoteFromDB );

Granted it cannot be produced the identical in PHP, Let me have a similar functionality. All the good examples I have seen create 2 different arrays either to replace the term having a predetermined string or replace the entire word completely. Individuals techniques aren't dynamic enough.

It is possible to method to create these kinds in both whole or like a method on PHP?