juust ~ php oddities

Unordered list of one element
  • rss
  • begin
  • about
    • vcard
    • WTF is BroJesus
  • php scripts
    • flickr wp widget
    • google multi key serp tool, php script
    • gwt plugin
  • php classes
    • php pagerank class
    • fibonacci class
    • robots.txt parser php class
  • serp
    • serp dashboard wordpress plugin
  • services

How to grab keywords from 7search

juust | 24/10/2008

“Seo tips and tricks” was not due til November, but this one just popped up today. I was looking for a tool to build a rapid keyword set for a blog, without doing extensive keyword research. The blackhat ’scraper’ scripts I found come up with ‘michigan seo’ far too many times :) so I built me a quick alternative.

How to grab keyword sets from 7Search

I want a set of keywords as blog categories to write a blog that contains material with the most popular keywords covering the whole active search pattern set. A nice tool for that is 7Search’s keyword tool.

It has a captcha protection, you have to answer it once and then you can query as much as you like, it shows last months top 100 search patterns with that keyword and the search volumes :

seo
1,991,112 $0.34 $0.33 $0.21 $0.09 $0.08
seo web design 8,085 $0.07 $0.02 $0.01
seo tool 2,647 $0.05 $0.02 $0.02 $0.01

As I am extremely lazy and hate typing data, I’ll make a quick script to cut and paste that list and have it magically transformed in a wordpress blog category list.

It turned out to be a simple one page program : source text file. Cut and paste stuff, I do a query on a keyword, select the result table area of the 7Search page (with the mouse : ) and paste it as text into my own form’s textarea, add the main key, and post it.

From the $_POST array, I take the textarea input and explode it on linebreaks. To get the keywords, I check for the first occurrence of 0-9, take the part that comes before it, and have the keywords.

In this function I test for the first 0-9. Had I stopped at the first number and started at 0, I would get thrown out of the loop if there is any 0 in the line (or 1, 2, 3…), regardless of there being any number before the first 0 :

  1. $pos = strpos($linesarr[$x], $i);
  2. if($pos >0) {
  3.     if($pos< $minpos) {
  4.         $mykeys = substr($linesarr[$x],0,$minpos);
  5.         echo $mykeys;
  6.         break;
  7.  }}}

So I test for the first 0 and store the position in minpos, then test for the first 1, 2, 3…, if it comes before the first 0, minpos is set to the lowest position.

  1. $lines=$_POST['textarea'];
  2. $linesarr = explode("\r\n", $lines);
  3.  
  4. for ($x=0;$x<count ($linesarr);$x++) {
  5. //set minpos to the length of the line
  6.     $minpos=strlen($linesarr[$x]);
  7.  
  8. //check numbers 0-9
  9.     for($i=0;$i&lt;10;$i++) {
  10. //get position of first number $i in the line
  11.         $pos = strpos($linesarr[$x], $i);
  12.         if($pos >0) {
  13.             if($pos< $minpos) $minpos=$pos;
  14.         }
  15.     }  
  16.  
  17. //is minpos smaller than the length of the line ? then its valid data
  18.         if($minpos<strlen($linesarr[$x])) {
  19.             $mykeys = substr($linesarr[$x],0,$minpos);
  20.             echo $mykeys;
  21.         }
  22. }

That way I always get the first number in the line and the part before it is the whole keyword text.

I also want the search volumes, which is the first full string after the keywords up till the first $-dollar sign. The minpos counter is already at the start digit of the volume. I can get the position of the first dollar sign, and trim off the blanks.

  1. //volume is the is at the start of the string after minpos
  2.  $volstr = trim(substr($linesarr[$x], $minpos));
  3. //and before the first dollar sign
  4.  $volcut = strpos($volstr, "$");
  5. //it contains "," : 9,111,222 so filter out the nonsense for mysql :
  6.  $vol = preg_replace('/,/', '', trim(substr($volstr, 0, $volcut)));
  7.  
  8.  if($minpos<strlen ($linesarr[$x])) {
  9.     $mykeys = substr($linesarr[$x],0,$minpos);
  10.     echo $mykeys."_".$vol;
  11.         }

[After this I stuff the data in a mysql table `sevencats`].

How to add a keyword list to wordpress as categories

Let's add the keywords to a wordpress blog as categories. Wordpress has a very simple function for it wp_insert_term in the taxonomy.php file.

In wpmu you do first have to pick the target blog, as you work on a blogs tableset, wp1_, wp2_ etcetera and if you start it up you get the admin users main blog as active tableset. If you want to add data like categories in another blogs taxonomy table you have to switch to that table set first.

  1. function connect_data() {
  2.   $DB_USER =  "";
  3.   $DB_PASSWORD = "";
  4.   $DB_HOST = "";
  5.   $DB_DATA = "";
  6.   $link =  mysql_connect($DB_HOST, $DB_USER, $DB_PASSWORD) or $error = mysql_error();
  7.   if (!$link) {
  8.       return $error;
  9.   }  
  10.         mysql_select_db($DB_DATA, $link) or $error = mysql_error();
  11.   return $link;
  12.  }
  13.  
  14. //link
  15. $cats=connect_data();
  16.  
  17. //get array with categories
  18. $categories=array();
  19.  
  20. $qry="SELECT cat FROM `sevencats`";
  21. $lst=mysql_query($qry, $cats) or die('list error '.mysql_error());
  22. while($row=mysql_fetch_assoc($lst)) {
  23.  $categories[]=$row['cat'];
  24. }
  25. //close db connection
  26. mysql_close($cats);
  27.  
  28. //open wordpress connection
  29. include_once('wp-config.php');
  30. include_once('wp-includes/wp-db.php');
  31. include_once('wp-includes/taxonomy.php');
  32.  
  33. //select target blog by id
  34. switch_to_blog(3);
  35.  
  36. //insert categories
  37. for ($i=0;$i<count ($categories);$i++) {
  38.       wp_insert_term($categories[$i], 'category');
  39. }
  40.  
  41. //switch back to users main blog
  42. restore_current_blog();

For a normal wordpress install you'd not have to switch blogs :

  1. //open wordpress connection
  2. include_once('wp-config.php');
  3. include_once('wp-includes/wp-db.php');
  4. include_once('wp-includes/taxonomy.php');
  5.  
  6. //insert categories
  7. for ($i=0;$i<count ($categories);$i++) {
  8.       wp_insert_term($categories[$i], 'category');
  9. }

That gets me the top 100 searches of last month as categories for my new blog all. You can fiddle with it a bit and only pick searches with a volume above 2000 monthly searches (just in case you want to go scraping and only want material that gets you in the serp pages for the high volume search terms).

Next edition : Red Hat Seo (with jingle bells) the Christmas Special :)

Categories
seo tips and tricks, wordpress
Tags
seo tips and tricks, wordpress
Comments rss
Comments rss
Trackback
Trackback

« zend php and google webmaster api II : wordpress mu auto-register xml rpc – remote posting with incutio wordpress ixr class »

One Response to “How to grab keywords from 7search”

  1. wow power leveling says:
    30/06/2009 at 5:03 am

    This is a great article. I’m new to blogging but still learning. Thanks for the great resource.

    Reply

Leave a Reply

Click here to cancel reply.

Recent Posts

  • geert wilders
  • gone till september
  • socialize me
  • Pagerank sculpting session
  • wish you were here

click me!
rss
Comments rss
Blog Directory
Web Developement Blogs - BlogCatalog Blog Directory
Listed in LS Blogs the Blog Directory and Blog Search Engine
Blog Flux Directory
joopita.com free web directory and search engine
design by jide
sitemap
22286 confirmed spam kills