Jump to content

php code to find related keywords


Jeffro

Recommended Posts

Here's a neat little script that you simply use by calling yourscript.php?microsoft (where microsoft is any keyword you want) and it returns 100 related keywords.  It's pretty raw in it's form.  I didn't write it. 

Purely for learning, I'm trying to figure out how to

1) issue a line break between each item in the elements of the arry? 

2) return only the keywords and not the associated array info. 

 

Currently, it returns results like:

Array ( [0] => microsoft [1] => microsoft security essentials [2] => microsoft office [3] => microsoft templates [4] => microsoft updates [5] => microsoft silverlight [6] => microsoft word [7] => microsoft money [8] => microsoft office 2010 [9] => microsoft clip art [10] => microsoft [11] => microsoft security essentials [12] => microsoft office [13] => microsoft templates [14] => microsoft updates [15] => microsoft silverlight [16] => microsoft word [17] => microsoft money [18] => microsoft office 2010 [19] => microsoft clip art [20] => microsoft security essentials [21] => microsoft security essentials review [22] => microsoft security essentials alert [23] => microsoft security essentials update [24] => microsoft security essentials 2.0

 

I'm wanting to know how to make it return results like...

microsoft

microsoft security essential

microsoft office

microsoft templates

etc....

 

 

 

<?php
function text_between($start,$end,$string) {
  if ($start != '') {$temp = explode($start,$string,2);} else {$temp = array('',$string);}
  $temp = explode($end,$temp[1],2);
  return $temp[0];
}
function gsscrape($keyword) {
  $keyword=str_replace(" ","+",$keyword);
  global $kw;
  $data=file_get_contents('http://clients1.google.com/complete/search?hl=en&q='.$keyword);
  $data=explode('[',$data,3);
  $data=explode('],[',$data[2]);
  foreach($data as $temp) {
  $kw[]= text_between('"','"',$temp);
  }
}
#simple to use, just use yourscriptname.php?keywords
if ($_SERVER['QUERY_STRING']!='') {
  gsscrape($_SERVER['QUERY_STRING']);
  foreach ($kw as $keyword) {
  gsscrape($keyword);
  }

#all results are in array $kw...
print_r($kw);
}
?>

Link to comment
Share on other sites

Really?

 

foreach ($kw as $keyword) {
  echo "$keyword <br/>";
}

 

Yep.. I made that way harder than it needed to be. 

 

And yes, really.  I'm not a programmer.. just trying to pick up little bits and learn to edit my own pages. 

 

Thanks for the help. 

Link to comment
Share on other sites

Even though the poster before beat me, I added sorting and removed duplicates to this as well.

 

Pretty neat script, you just place a keyword after the scripts name in the url

like:

http://mysite.com/this-script-name.php?games

 

<?php
function text_between($start,$end,$string) {
  if ($start != '') {$temp = explode($start,$string,2);} else {$temp = array('',$string);}
  $temp = explode($end,$temp[1],2);
  return $temp[0];
}
function gsscrape($keyword) {
  $keyword=str_replace(" ","+",$keyword);
  global $kw;
  $data=file_get_contents('http://clients1.google.com/complete/search?hl=en&q='.$keyword);
  $data=explode('[',$data,3);
  $data=explode('],[',$data[2]);
  foreach($data as $temp) {
  $kw[]= text_between('"','"',$temp);
  }
}
#simple to use, just use yourscriptname.php?keywords

if ($_SERVER['QUERY_STRING']!='') {
  gsscrape($_SERVER['QUERY_STRING']);
  foreach ($kw as $keyword) {
  gsscrape($keyword);
  }

//sorted and duplicates removed
sort(array_unique($kw));

#all results echoed with break
foreach ($kw as $keywords) {
echo $keywords. "<br />";
}

}
?>

Link to comment
Share on other sites

Even though the poster before beat me, I added sorting and removed duplicates to this as well.

 

Pretty neat script, you just place a keyword after the scripts name in the url

like:

http://mysite.com/this-script-name.php?games

 

<?php
function text_between($start,$end,$string) {
  if ($start != '') {$temp = explode($start,$string,2);} else {$temp = array('',$string);}
  $temp = explode($end,$temp[1],2);
  return $temp[0];
}
function gsscrape($keyword) {
  $keyword=str_replace(" ","+",$keyword);
  global $kw;
  $data=file_get_contents('http://clients1.google.com/complete/search?hl=en&q='.$keyword);
  $data=explode('[',$data,3);
  $data=explode('],[',$data[2]);
  foreach($data as $temp) {
  $kw[]= text_between('"','"',$temp);
  }
}
#simple to use, just use yourscriptname.php?keywords

if ($_SERVER['QUERY_STRING']!='') {
  gsscrape($_SERVER['QUERY_STRING']);
  foreach ($kw as $keyword) {
  gsscrape($keyword);
  }

//sorted and duplicates removed
sort(array_unique($kw));

#all results echoed with break
foreach ($kw as $keywords) {
echo $keywords. "<br />";
}

}
?>

 

Nice addition.  Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.