Tuesday, January 29th, 2008

PHP Function: Word Filter

So, last night I was mainly experimenting with Whird, more specifically I was trying to build an internal search feature. This is something I've been putting off for long enough, and I'd really like to get it coded up. I didn't finish it last night, but at least I've made a start.

Anyhow, during my experimentation I wrote this little PHP function for filtering words out of a string. Basically the function takes 2 strings as arguments before filtering words out of the first string based on words found in the second. I've posted it below for future reference:

function word_filter($string1,$string2){
    $string1 = trim($string1);
    $string1 = preg_replace('/s+/', ' ', $string1);
    $string1 = explode(" ",$string1);
    $wordcount = count($string1);
    $i = 0;
    while ($i < $wordcount) {
        $string = $string1[$i];
        if (strstr(strtolower($string2),strtolower($string))) {
            $string1[$i] = "";
        }
    $i++;
    }
$string1 = implode(" ",$string1);        
return($string1);
}

Example usage

This is probably not the best example, but this:

$poem = <<<EOD
<p><em>GIVE me women, wine, and snuff <br />
Untill I cry out "hold, enough!" <br />
You may do so sans objection <br />
Till the day of resurrection: <br />
For, bless my beard, they aye shall be <br />
My beloved Trinity.</em></p>
EOD;

$common_words = 'and be but do for is it may me my not of the they there so was you';

echo word_filter($poem,$common_words);

Would output something like this:

GIVE women, wine, snuff
Untill cry out "hold, enough!"
sans objection
Till day resurrection:
For, bless beard, aye shall
beloved Trinity.

John Keats would be proud, not! Please feel free to optimise, or let me know if a one line equivalent already exists :)

Tagged with: code, php, projects, whird


4 Responses to “PHP Function: Word Filter”

  1. Vincent wrote,

    Well, basically, I'd recommend you to go with an existing searching+indexing system as it is likely to be far more efficient. The Zend Framework provides an excellent search module (Zend_Search_Lucene) that can also be used separately. You'd first have to download the entire Zend Framework but then you can just take the /library/Zend/Search/Lucene.php file and the /library/Zend/Search/Lucene directory, include the former, and then you can work with it just as described in the documentation. Really, this excellent module is highly recommended.

  2. Philip wrote,

    Well, basically, I'd recommend you to go with an existing searching+indexing system as it is likely to be far more efficient.

    Erm, thanks for the vote of confidence! :D

    Seriously though, I'll look into it thanks. However, I'm pretty sure I'll end up coding the search feature myself. I know I've been putting off, but I'm actually looking forward to writing it. It may not end up as efficient, or even as good, but I enjoy a challenge. Besides, I'm not a big fan of frameworks, at least not for my own projects.

  3. Snuff Store wrote,

    Got no idea about php aspect of this, but thanks for posting one of my favourite 'snuff' poems - they certainly are 'my' blessed trinity..!

  4. Philip wrote,

    @Snuff Store: LOL, I didn't even know people still used the stnuff!

Add Your Comment

Use the form below to add your comment. Markdown syntax is available. Note, comments are moderated by me for spam filtering. Alternatively, feel free to contact me privately.