Keyword generating is one of the important parts for advanced searching. You may have already used the LIKE query in MySQL for searching in PHP. But it’s very limited. In many cases, the LIKE command will not give us the expected output. In order to do such complex searching, we have to generate keywords. These are techniques used by almost all search engines or even a good search functionality in a website. Please note that this a PHP script for generating keywords from a given string. It will not do any searching part.
Why should you use Keyword Generator for Searching
For example, consider the case, a database has a value “programmer“.
I use the query: “SELECT * FROM table where column LIKE ‘%programmers%‘”
What it will return is zero number of rows. In order to make it work, we have to remove the ‘s’ at the end of programmers. I hope you got a rough I idea. But it’s a simple one. What if our database has a value “Programmer A” and a user searches for “Programmer A “? The result will be zero. Because there are two spaces in between Programmer and A. And space after A.
So in these searching algorithms, if you use LIKE command, you may lose the important ones that you have to display to the user. Here comes our PHP script, advanced keyword generator. What it do is generate keywords by going through some algorithms and functions. I have described it in detail below.
PHP Script for Advanced Keyword Generator
The below code may a looks simple, few lines of code. But the included libraries are very lengthy. Try the demo first.
require('stemmer.php'); // stemming algorithm php library
require('stopwords.php'); // array of stop words
$str = strtolower($str); // converting to lowe case
$str = preg_replace("/[^a-z]+/"," ",$str); // only characters from a-z
$str = removeCommonWords($str); // remove stop words
$str_arr = explode(' ',$str);// separaing each words in to array
$stemmed = '';
foreach($str_arr as $new)
$str = PorterStemmer::Stem($new); // stemming each word
$stemmed = $stemmed." ".$str; // adding it to a string
$arr = explode(" ",$stemmed); // separaing each words in to array
$arr = array_unique($arr); // selecting only the unique words from array
$str = implode(" ",$arr); // combine words in array to a sting
echo getKeywords("Hello I am a programmer");
Features of Advanced Keyword Generator
Stemming is one of the important parts of searching. What stemming programs do is convert words like “programmers” “programmer” “programming” “programmed” -> “program” (check demo or below for more). The best stemmer or stemming algorithm is available it porter stemmer algorithm and we are using that one in our script. You can see is a demo of it here.
Some examples of stemming
- skills -> skill
- skilled -> skill
- weakness ->weak
- requires -> requir
- temptations -> temptat
- seeking – seek
It may sometimes produce words that are not in a dictionary. But trust me, it will be great on searching.
Removing Stop Words
You may be already heard of it, there no need of typing is, and, for, were etc while searching in Google. The reason is that Google removes these types of words. (They are called stop words.) It makes our searching more efficient. We have created an array with 600+ stop words and have included that in our script.
- Converting to lower case
- Removing special characters, numbers
- Removing multiple words
- Removing multiple white spaces