0

これに少しこだわって、私がやろうとしているのは、私のサイトに戻るリンクを含む URL のリストをループすることです。リンクを生成するために使用される HTML コードをキャプチャし、代わりに使用されるアンカー テキストを保存しようとしています。リンク、

[マーティによって削除されたコードは以下を参照]

そのため、マーティリンクに使用されるコードは、まだ構築しようとしている関数を使用しています。

これは私の find_marty_links 関数です

function find_marty_links($file, $keyword){
    //1: Find link to my site <a href="http://www.***martin***-gardner.co.uk" target="_blank" title="Web Developer">Web Developer</a>
    //2: copy the FULL HTML LINK to array
    //3: copy the REL value? NOFOLLOW : FOLLOW to array
    //4  copy TITLE (if any) to array
    //5  copy Anchor Text to array

    $htmlDoc = new DomDocument();
    $htmlDoc->loadhtml($file);

    $output_array = array();
    foreach($htmlDoc->getElementsByTagName('a') as $link) {

            // STEP 1
        // SEARCH ENTIRE PAGE FOR KEYWORD?
            // FIND A LINK WITH MY KEYWORD?
            preg_match_all('???', $link, $output); //???//

            if(strpos($output) == $keyword){


               // STEP 2
               // COPY THE FULL HTML FOR THAT LINK?
               $full_html_link = preg_match(??);
               $output_array['link_html'] = $full_html_link;

               // STEP 3
               // COPY THE REL VALUE TO ARRAY
               $link_rel = $link->getAttribute('rel');
               $output_array['link_rel'] = $link_rel;

               // STEP 4
               // COPY TITLE TO ARRAY
               $link_title = $link->getAttribute('title');
               $output_array['link_title'] = $link_title;

               // STEP 5
               // COPY ANCHOR TEXT TO ARRAY
               $anchor_exp = expode('>'); //???
               $anchor_txt = $anchor_exp[2];//??
               $output_array['link_anchor'] = $anchor_txt;

            }

    }
}

!!アップデート!! 以下のような配列を生成する必要があります

$results = array('link_html' => '<a title="test" href="http://site.com" rel="nofollow">anchor text</a>',
                 'link_rel' => 'nofollow',
                 'link_title' => 'test',
                 'link_anchor' => 'anchor text'
                 )

助けてくれてありがとう..

M

4

1 に答える 1

1

更新されたコードは次のとおりです。

function find_marty_links($file, $keyword){
    $htmlDoc = new DomDocument();
    $htmlDoc->loadhtml($file);
    $links = array();

    foreach($htmlDoc->getElementsByTagName('a') as $link) {
        $url = $link->getAttribute('href');
        $title = $link->getAttribute('title');
        $text = $link->nodeValue;
        $rel = $link->getAttribute('rel');

        if(strpos($url,$keyword) !== false || strpos($title,$keyword) !== false || strpos($text,$keyword) !== false)
        {
            $links[] = array('url' => $url, 'text' => $text, 'title' => $title, 'rel' => $rel);
        }
    }

    return $links;
}
于 2012-07-20T13:22:29.347 に答える