2011-05-14 10:40



I need help understanding how to implement this part of PHP code in Ruby. Before thinking, "OMG! THIS IS A LOT OF CODE," I just want to let you know the tiny section below is the relevant code to the question, but I included more below as I have the tendency to not include important facts in my questions (I'm a noob.) This script is for a SERP checker with the aim to teach me to program :

        $page = ob_get_contents();

        $page = str_replace('<b>','',$page);
        $page = str_replace('</b>','',$page);
        //preg_match('/008000\">(.+)<\/font><nobr>/i', $page, $match);
        preg_match_all('/<font color=#008000>(.*)<\/font>/', $page, $match);
        $r = 0;
        $position = '0';

My Ruby code is as follows:

def clean_up_keywords(str)
", ",").delete("").split(',')

def clean_up_list(arr)

def make_strings_url_friendly(arr)
  arr.each do |e|
    e.gsub!(" ", "+")

def make_urls(arr) {|e| "" + e}

post '/ranked' do
  dirty_list = clean_up_keywords(params[:keyword])
  clean_list = clean_up_list(dirty_list)
  url_ready_list = make_strings_url_friendly(clean_list)
  url_list = make_urls(url_ready_list)

The entire PHP script can be found here:

The entire Ruby script can be found here:

My deal is that I was told I don't really have to implement the output buffer part because it's Ruby, which is great for me because I can't make heads or tails as to what it is even after several people explaining it to me. (Someday)

However, in the output buffer section, the $page variable is created. It is then used in the next section where it appears that it is removing bold. Does my Ruby script already take care of this?

Then, the SERP checker appears to be looking for results with that font color -- and then what? putting them in an array called $match?

I was thinking, instead of having my code search for a font color, to have it search for the tag in the SERPs as it appears to be the only place where Google uses the cite thing... because the font tag type of search seems kind of deprecated to me.

I'm hoping that any one of you can tell me whether or not I am understanding this PHP code correctly and can give me a hint or two as to how to implement it in Ruby. My main issue is really knowing which elements of the PHP to NOT use since that whole output buffer thing has me baffled. Anything that points me in the right direction is much appreciated.

Also, in the original PHP code, it makes the Google urls like this (pseudocode):

"" +keyword+ "&btnG=Search"

But in my Ruby, I just made it like this:

"" +keyword

Does not adding the "&btnG=Search" to the end of the url make a difference? When I manually enter either url into my browser, it takes me to the same place, but I am unsure whether, programmatically, it makes a difference.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongtaihui5131 dongtaihui5131 10年前

    It pulls the page into a variable, strips all the bold tags, then puts all the green text from the page (without the coloring) into the $matches array.

    点赞 评论 复制链接分享