DeTypo in PHP

Sometimes we need to detect mistypes :)
We can use google-search to make sure if given word is a mistype, here my solution:

  1. if($_GET['key']!=''){
  2.   $key=$_GET['key'];
  3.   $s=GetData('http://www.google.com/search?q='.urlencode($key));
  4.   if(strstr($s, '&spell=1 class=p><b><i>')){
  5.     $s=substr($s, strpos($s, '&spell=1 class=p><b><i>')+strlen('&spell=1 class=p><b><i>'));
  6.     $s=substr($s, 0, strpos($s, '</a>'));
  7.     $key=strip_tags($s);
  8.   }
  9.  
  10.   echo $key;
  11. }
  12.  
  13. function GetData($url){
  14.  $ch=curl_init();
  15.  curl_setopt($ch, CURLOPT_URL, $url);
  16.  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  17.  curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  18.  $out=curl_exec($ch);
  19.  curl_close($ch);
  20.  
  21.  return $out;
  22. }

Using:
http://site.com/detypo.php?key=wordl


Tags: , , , ,