开发者

Understanding some code in PHP

I am a noob programmer who is learning Ruby and I decided to write a SERP checker. The thing is that I'm so noob that I don't know how to go about it so I found a script online that does it, but it's written in PHP. A friend helped me understand the first part of it, where he found a lot of problems with the script. However, as my friend is busy, he cannot help me "decipher" the second part of the script.

I was able to write, in Ruby, up until the $i = 0 and now I'm stuck. I'm trying to figure out what's going on in the rest of the script. There are these lines, in particular that baffle me, I think I can handle the rest, but have included it all (at the bottom), in the case I am not giving enough information.

            $keyword_implode = str_replace(' ','+',$keywords[$i]);
        $fetch_url = "http://www.google.com/search?num=50&q=" . $keyword_implode . "&btnG=Search";
        ob_start();
        include_once($fetch_url);
        $page = ob_get_contents();
        ob_end_clean();  

I looked on php.net for some information on str_replace and none of the examples really resemble (to me) any of the examples, so I'm trying to figure out what he's trying to replace with what.

Here is the whole script:

<?php
    if ($_POST['url'] != '') {
        if (strpos($_POST['keyword'],"\n")) {
            $keywords = str_replace("\n",",",$_POST['keyword']);
            $keywords = explode(",",$keywords);
        } else {
            $keywords[0] = $_POST['keyword'];
        }
        $keyword_input = $_POST['keyword'];
        $url = $_POST['url'];
    }

    $i = 0;

    if ($keywords[$i] != '') {
        while ($keywords[$i] != '') {
            $keyword_implode = str_replace(' ','+',$keywords[$i]);
            $fetch_url = "http://www.google.com/search?num=50&q=" . $keyword_implode . "&btnG=Search";
            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';
            while ($match[0][$r] != '') {
                if ($position == '0') {
                    if (strpos($match[0][$r],$url)) {
                        $position = $r+1;
                    } 
                }
                $r++;
            } 
            $google_position = $position;

            $keyword_table .= '
                <tr>
                    <td>' . $keywords[$i] . '</td>
                    <td>' . $google_position . '</td>
                </tr>';
            $i++;
        }
        $keyword_table = '
            <table class="result-table" cellspacing="1">
                <tr>
                    <th>Keyword</th>
                    <th>Google</th>
                </tr>' . $keyword_table . '
            </table>';
    }
?>

Here's what I've written so far, in Ruby:

require 'sinatra'
require 'rspec'
get '/serp_checker' do
  "<form action='/ranked' method='post'>
  <label for='keyword'>Keyword</label>
  <textarea name='keyword' id='keyword' type='text' /></textarea>
  <label for='url'>URL</label>
  <input name='url' id='url' type='text' />
  <input type='submit' value='Go!' />
  </form>"
end

def clean_up_keywords(str)
  str.gsub("\n", ",").delete("\r").split(',')
end

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

post '/ranked' do
  dirty_list = clean_up_keywords(p开发者_高级运维arams[:keyword])
  clean_list = clean_up_list(dirty_list)
  return clean_list.to_s
end

Sorry about this being so long, I just wanted to describe the whole problem, so that I don't forget to give out important information about the problem.


Just looking at the section of code you've indicated at the top of your post:

  • The first line is replacing any spaces with a + symbol in whatever is inside that part of the array.
  • $fetch_url builds a url using the altered string from the first line.
  • ob_start activates the output buffer, which prevents anything from being outputted to the page.
  • include_once is including the URL built above (if not already included).
  • $page = ob_get_contents is assigning the content of the output buffer to the $page variable.
  • ob_end_clean wipes the output buffer.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜