Add Spell Check With PHP Using Google

PHP

Read {count} times since 2020

If you google something that has spelling mistakes, Google will automatically correct it for you or if the bot isn’t sure, it will give the Did You Mean ? clause. If you’re developing a search engine for your site, this spell check will come in handy. If the user had made a mistake and he was shown a “No Results Found” page, user will be annoyed. So, it’s better to have a spell check for your site.

Google until 2013 had the Spell Check service available to all developers by accessing a URL. Sadly, they discontinued and we were forced to look up on other methods. Bing supports Spell check and Yahoo too. But it’s not as good & perfect as Google.

Google Translate also has the Did You Mean functionality. So, I looked at the AJAX request and response and got the URL for getting the suggestions. It’s simple, we’ll send the request with the word to get suggestions. If there is one, we will display it as “Did you mean {word} ?”. We’ll use cURL and RegEx for accomplishing our task.

Note that this trick or “hack” is not publicly made available by Google and I’m not responsible for the actions you make with this trick and thank you Google for your awesome products and requests like this.

Here is the function that we’ll use to check with Google about the word and what the Google Dictionary Bot thinks. If there isn’t any thing it’ll return null and if there is, it’ll return the word as corrected by Google.

It’s very necessary that your web server supports cURL.

UPDATE

Spell Check is now under The Francium Project and it has become a static class.

Usage

The usage of the function \Fr\SC::check() is very easy, you just pass the word to it and it will return the corrected word. It won’t return anything  if the word you gave is of more than the length 49. So that’s the limit.

In this example, I give the wrong word “googel” and it will output “google”.

$word = \Fr\SC::check("googel");
if($word == null){
 echo "No Suggestion";
}else{
 echo "Corrected Word - " . $word;
}

Here, I give a sentence of exactly the length 49 to the function and it’ll give the corrected output :

$word = \Fr\SC::check("soemthign's wrnog. If there's omething wqrong wit");
if($word == null){
 echo "No Suggestion";
}else{
 echo "Corrected Word - " . $word;
}

Ain’t that cool ? It is very strongly recommended that you include this trick on your search engine, because it will increase page visits and user’s experience with your site.

Show Comments