Jeffro Posted April 27, 2011 Share Posted April 27, 2011 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234901-php-code-to-find-related-keywords/ Share on other sites More sharing options...
gizmola Posted April 27, 2011 Share Posted April 27, 2011 foreach Quote Link to comment https://forums.phpfreaks.com/topic/234901-php-code-to-find-related-keywords/#findComment-1207186 Share on other sites More sharing options...
Jeffro Posted April 27, 2011 Author Share Posted April 27, 2011 foreach I got that far before posting here. Nothing worked for me though. Quote Link to comment https://forums.phpfreaks.com/topic/234901-php-code-to-find-related-keywords/#findComment-1207192 Share on other sites More sharing options...
gizmola Posted April 27, 2011 Share Posted April 27, 2011 Really? foreach ($kw as $keyword) { echo "$keyword "; } Quote Link to comment https://forums.phpfreaks.com/topic/234901-php-code-to-find-related-keywords/#findComment-1207197 Share on other sites More sharing options...
Jeffro Posted April 27, 2011 Author Share Posted April 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234901-php-code-to-find-related-keywords/#findComment-1207208 Share on other sites More sharing options...
QuickOldCar Posted April 27, 2011 Share Posted April 27, 2011 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 />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234901-php-code-to-find-related-keywords/#findComment-1207216 Share on other sites More sharing options...
Jeffro Posted April 27, 2011 Author Share Posted April 27, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/234901-php-code-to-find-related-keywords/#findComment-1207233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.