Skip to content

Instantly share code, notes, and snippets.

@berdyshev
Created June 28, 2013 14:27
Show Gist options
  • Save berdyshev/5885078 to your computer and use it in GitHub Desktop.
Save berdyshev/5885078 to your computer and use it in GitHub Desktop.
Please create a simple function in procedural PHP coding. The function will have to get the words given into an array(feel free to chose the kind), and for each one check if a stop word is found. In case the stop word is found the function will have to replace the characters with asterisks. The stop words are located into the DB in a table named…
<?php
/**
* Filter stop words in the given array of words.
*
* @param array $words Array of words to filter.
* @return array
* New array with filtered words (replaced with asterisks).
*/
function filter_stop_words($words) {
$db = new PDO('mysql:host=localhost;dbname=sites_sandbox;charset=utf8', 'root', '123Qwe');
$query = $db->query('SELECT stop_word FROM stop_words INNER JOIN common_words ON common_words.ID_stop_word != ID');
$stop_words = $query->fetchAll(PDO::FETCH_COLUMN, 0);
$replacements = array_map(function($item) {
return str_repeat('*', strlen($item));
}, $stop_words);
return str_replace($stop_words, $replacements, $words);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment