dongtaigan1594 2011-05-14 10:40
浏览 23
已采纳

需要帮助理解PHP代码,以便我可以在Ruby中重写

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 :

        ob_start();
        include_once($fetch_url);
        $page = ob_get_contents();
        ob_end_clean();  

        $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)
  str.gsub("
", ",").delete("").split(',')
end

def clean_up_list(arr)
  arr.reject(&:empty?).each(&:lstrip!)
end

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

def make_urls(arr)
  arr.map {|e| "http://www.google.com/search?num=100&q=" + e}
end

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)
end

The entire PHP script can be found here: http://pastie.org/1899806

The entire Ruby script can be found here: https://github.com/MelanieS/RankyPanky/blob/master/lib/rankypanky.rb

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):

"http://www.google.com/search?num=50&q=" +keyword+ "&btnG=Search"

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

"http://www.google.com/search?num=50&q=" +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.

  • 写回答

1条回答 默认 最新

  • dongtaihui5131 2011-05-14 10:45
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路